views:

164

answers:

4

is there a way to call a program from python without waiting for it to return? i created a script which copies a program to a directory and runs that program. but when i call the program from python, the python script does not exit until the program i launched exits. i have tried os.system and Popen. is there another way to do this?

Added info: os.spawnl with os.P_DETACH still doesn't work; according to the docs, "P_DETACH is similar to P_NOWAIT, but the new process is detached from the console of the calling process". but it is still somehow attached to my calling process (calling script won't quit until any of the called executables return)

Program:

os.system("start test.exe")
print "Done"

after it executes test.exe, it prints Done. but it does not terminate the script's execution (script process still running). tried creating a daemon thread and Popen with a P_DETACH, still no go.

+1  A: 

By using poll() instead of wait() on Popen it will not block and it won't wait for the program to run. However, I think the only way to really stop the entire program from waiting is by creating a daemonic thread which starts the process. That way you'll never have to wait for it.

class MyThread(threading.Thread):
    def run(self):
        '''Start your thread here'''
        pass

thread = MyThread()
thread.daemon = True
thread.start()
WoLpH
thanks. i have tried using subprocess, but when i set shell=True on Popen, it runs perfectly under eclipse+pydev but won't run when built as an executable on py2exe. i'm guessing there is a conflict with my py2exe built with 32-bit python and the actual command being called built as 64-bit.anyways, i can maybe work around that. the script still isn't quitting, though.
maranas
@maranas: in that case you could simply create a new thread and spawn the process from there. Set `thread.daemon` to `True` and it won't stall your process on exit :)
WoLpH
thanks again for the quick reply. it should work, but somehow the main script is not terminating. it just executes to completion but it does not terminate
maranas
A: 

There's a similar question already on stackoverflow. Does that help?

Jon Cage
nope. it creates a new process but the main program (python script) does not exit. Edit:
maranas
A: 

Under Windows, if you invoke the program using the shell START command you should be able to "release" the parent process and allow it to exit. Try START /? at the DOS prompt to learn more.

Peter Hansen
tried doing this, but it still doesn't work. tried it with Popen and os.system.
maranas
Well, start *does* work, and should (I believe) how I described, so without either sample code or a description of in what way it doesn't work, I can't help more. I mean, does it launch your program but your Python app can't exit? Or you can't even get it to start your program? What arguments did you pass it?
Peter Hansen
start does work. but using it with python doesn't, somehow. edited the question, but just to clarify, the python program is: <p>os.system("start test.exe")<p>print "Done"<p>it starts test.exe, and Done is printed. but the script doesn't stop execution, it seems the process is still tied to test.exe.
maranas
It must be something special in the nature of your test.exe program. I've tried this in various ways here, and START always does precisely what you say you want... unfortunately I guess you'd have to use my computer to get the same results. ;-)
Peter Hansen
+2  A: 

I posted a solution how to detach a process in a previous question.

newtover
this worked. sorry to not recognize it from the previous question; i thought the answer was beyond the previous question's scope.
maranas
another note: i accepted the answer because it worked, but i noticed another possible "quirk" with developing py2exe apps in pydev+eclipse. i was able to tell that the main script was not exiting only because eclipse's output window was not terminating; even if the script executes to completion it is still waiting for returns. but, when i tried compiling to a py2exe executable, the expected behavior occurs (runs the processes as detached, then quits). i am not sure, but the executable name is not in the process list anymore. this works for all approaches (os.system("start *", subprocs, etc.)
maranas
@maranas: could you post some details about your quirk in a comment to my answer in the neighbor question. This info might be interesting to someone searching.
newtover