LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2013, 06:09 AM   #1
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Rep: Reputation: Disabled
Subprocess in Python giving error


Hello,
I am a beginner in Python scripting. I tried run/executing two programs simultaneously and it gave following error after printing few output lines.
Code:
Starting with the Basic code

a =  -2

b =  0.0

c =  0.0

total =  0

1360238044.98

a =  -2.0

b =  1.02

c =  0.51

total =  0.0

1360238048.01

Traceback (most recent call last):
  File "C:/AKS/Subprocess/ex_prog01.py", line 43, in <module>
    root = Myprog()
  File "C:/AKS/Subprocess/ex_prog01.py", line 9, in __init__
    self.Task_03()
  File "C:/AKS/Subprocess/ex_prog01.py", line 38, in Task_03
    q = subprocess.Popen(self.Task_02())
  File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 852, in _execute_child
    args = list2cmdline(args)
  File "C:\Python27\lib\subprocess.py", line 580, in list2cmdline
    for arg in seq:
TypeError: 'NoneType' object is not iterable




i am using python 2.7 and is installed on windows 7 machine.
Following is the code which i have tried with

Code:
import subprocess, time


class Myprog:

    def __init__(self,master=None):
        self.Task_03()
       

    def Task_01(self):
        v1 = 'Hello there'
        for x in range(1):
            time.sleep(2)
            print "\n", v1
        

    def Task_02(self):
        a = 1
        b = 1
        c = 1
        for i in range(2):
            total = ( a + b ) * c / (a + b + c)
            a = total - 2
            c = i * 0.51
            b = 2 * c
            time.sleep(3)
            print "\na = ", a
            print "\nb = ", b
            print "\nc = ", c
            print "\ntotal = ", total
            print "\n", time.time()
            

    def Task_03(self):
        print "\nStarting with the Basic code"
        q = subprocess.Popen(self.Task_02())
        p = subprocess.Popen(self.Task_01())
        print "\n\t\t\t\tEnd!!!!!"


root = Myprog()
root.mainloop()



Thanks in advance................

Last edited by B Akshay; 02-07-2013 at 06:21 AM.
 
Old 02-07-2013, 08:04 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

... The arguments to Popen are as follows.

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

in your case args is not a string and even, args is not iterable. that's why you got that error message. I do not understand how is it executed...
 
Old 02-07-2013, 03:27 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by B Akshay View Post
Code:
    def Task_03(self):
        print "\nStarting with the Basic code"
        q = subprocess.Popen(self.Task_02())
        p = subprocess.Popen(self.Task_01())
        print "\n\t\t\t\tEnd!!!!!"
Task_01 and Task_02 do not return strings or arrays, which is what subprocess.Popen is expecting.

I wrote this function to make it easy to call subprocess.Popen. It returns True or False plus the output of the command. It redirects stderr to stdout so that you can display error output if something goes wrong. The important part is the call to shlex.split which converts the string into an array.
Code:
def command(_cmd_):
    """
    Execute a command and return the results
    """
    # We have to use shlex.split because unless we specify "shell=True",
    # subprocess.call requires the command to be split into an array of
    # arguments. We DO NOT use "shell=True" because it could be used to run
    # malicious code.
    args = shlex.split(_cmd_)
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output, errors = p.communicate()
    if p.returncode < 0:
        output += "ERROR: Child was terminated by signal"
        return False, output
    else:
        if 0 != p.returncode:
            output += "ERROR: Child returned %d" % p.returncode
            return False, output

    return True, output
 
Old 02-08-2013, 01:23 AM   #4
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Thank you Pan64 for your inputs...........

David1357, i tried with the code given by you in my program, but still i am getting the following error,

Code:
Traceback (most recent call last):
  File "C:/AKS/Subprocess/ex_prog01.py", line 65, in <module>
    root = Myprog()
  File "C:/AKS/Subprocess/ex_prog01.py", line 9, in __init__
    self.Task_03()
  File "C:/AKS/Subprocess/ex_prog01.py", line 38, in Task_03
    p = subprocess.Popen(self.Task_02(), stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
  File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 852, in _execute_child
    args = list2cmdline(args)
  File "C:\Python27\lib\subprocess.py", line 580, in list2cmdline
    for arg in seq:
TypeError: 'NoneType' object is not iterable
>>> 
the modification in the code which i have done is as follows, (guess its correct)

Code:
def Task_03(self):
        print "\nStarting with the Basic code"
        p = subprocess.Popen(self.Task_02(), stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        output, errors = p.communicate()
        if p.returncode < 0 :
            output += "ERROR: Child was terminated by signal"
            return False, output
        else :
            if 0 != p.returncode:
                output += "ERROR: Child returned %d"
                return False, output

        return True, output

        
        q = subprocess.Popen(self.Task_01(), stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        output1, errors1 = q.communicate()
        if q.returncode < 0 :
            output1 += "ERROR: Child was terminated by signal"
            return False, output1
        else:
            if 0 != q.returncode:
                output1 += "ERROR: Child returned %d"
                return False, Output1

        return True, output1
        print "\n\t\t\t\tEnd!!!!!"

please suggest some thing........................
 
Old 02-09-2013, 02:22 AM   #5
B Akshay
Member
 
Registered: Sep 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
my main purpose is to run to different processes in parallel..

Please suggest how to do same in Python..


Regards,
B Akshay
 
Old 02-09-2013, 10:18 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by B Akshay View Post
my main purpose is to run to different processes in parallel..
I think you should learn what a process is first, it looks like maybe you actually want multiple threads.
 
Old 02-09-2013, 11:37 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
here is a simple tutorial about multithreading: http://www.tutorialspoint.com/python...ithreading.htm
and another one about multiprocessing: http://www.doughellmann.com/PyMOTW/m...ng/basics.html
 
Old 02-14-2013, 10:07 AM   #8
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by B Akshay View Post
David1357, i tried with the code given by you in my program, but still i am getting the following error,
please suggest some thing........................
As I said before, Popen expects either a string or an array of strings.

Your functions do not return anything, much less strings.

It really looks like you want to use the Python thread library. This will allow you to pass your functions as arguments. Remember to pass the function name, not the function result:
Code:
try:
   thread.start_new_thread(self.Task_01)
   thread.start_new_thread(self.Task_02)
except:
   print "Error: unable to start threads"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
subprocess installed post-installation script returned error exit status 127 Kaind Ubuntu 3 11-15-2012 06:48 PM
python os.system() or subprocess.Popen() TypeError on ubuntu lucid 10.04 sahar.gh Programming 1 06-25-2012 07:56 PM
[SOLVED] python 2.6.4 [make test] giving error: janakiramulu Linux From Scratch 2 05-22-2011 02:09 AM
dpkg -i goes wrong because of a piping error to/from subprocess wilsonsamm Linux - General 1 06-09-2008 10:51 PM
Error: subprocess dpkg-split killed by signal (Segmentation fault) Jinouchi Linux - Software 0 06-05-2008 11:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration