ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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()
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...
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
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!!!!!"
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.