Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call a program from python without waiting for it to return

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.

like image 585
maranas Avatar asked Apr 08 '10 17:04

maranas


People also ask

How do you wait for a command in Python?

You might need to wait for another function to complete, for a file to upload, or simply to make the user experience smoother. If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

How do you stop a Python executing the next line?

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program from running.

How do you force a program to end in Python?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.


1 Answers

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.

like image 91
Peter Hansen Avatar answered Sep 18 '22 14:09

Peter Hansen