Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python calling script without waiting for it to execute

I want to know if there is any way to call a python script and just completely start that programe in a different thread so the main programe (caller) does not have to wait till the called script finishes executing. So far I've tried subprocess, and os.system but both of these two lag on until the script finishes executing.

main.py

os.system('"F:\second.py"')
//continue the rest of the code without waiting for second.py to finish

second.py

//do stuff
like image 699
Elinoter99 Avatar asked Oct 11 '25 08:10

Elinoter99


1 Answers

you can try subprocess

first.py

import subprocess
subprocess.Popen(["python.exe", "second.py"])
print "done"

second.py

import time
for i in range(10):
    print i
    time.sleep(10)

EDIT:

if you want to completly seperate the first and the second script add a flag

first.py

import subprocess
subprocess.Popen(["python.exe", "second.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
print "done"
like image 152
Thomas Avatar answered Oct 14 '25 05:10

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!