#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    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................
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    The fragment you've posted is bizarre. However, I understand the error.
    subprocess documentation
    class subprocess.Popen(args, ...

    args should be a sequence of program arguments or else a single string.
    Either way, the first argument to Popen is iterable. You, however, passed the result of method Task_02. Task_02 doesn't have a return statement and therefore returns None . Because that's how python works. If you think your program did anything more than this up to the error
    Code:
    import time
    
    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()
    then you're just about wrong.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    507
    Rep Power
    33
    I tried run/executing two programs simultaneously
    Subprocess will not run 2 things at once. It will run the first, and when it is finished, run the second. Multiprocessing or threading will start more than one thing at a time.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Subprocess will not run 2 things at once---but you can cheat.
    In windows/unix you can run gui programs using
    start excel
    command &
    but anyway the code of original post surely looked like a smash of misunderstood concepts.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Thank you for your comments and input.

    And yes i am having a lot of misconception about this in Python.

    My aim is to run two or more process simultaneously(parallel), can you please suggest the way to do it in python.



    Regards.......
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    507
    Rep Power
    33
    Doug Hellmann's site is always a good place to try http://www.doughellmann.com/PyMOTW/m...ng/basics.html
    Last edited by dwblas; February 8th, 2013 at 09:43 AM.

IMN logo majestic logo threadwatch logo seochat tools logo