So I have the main python script and I want to call another python script from my main file, however, whenever I do this the script I call kinda overtakes the original. is there any way to call a python script in the background to have it not interrupt the main script in the console?
Asked
Active
Viewed 3,142 times
1
-
1Sure, learn about the `subprocess` module. – Błotosmętek Jul 30 '17 at 18:25
-
Possible duplicate of [Non blocking subprocess.call](https://stackoverflow.com/questions/16071866/non-blocking-subprocess-call) – Gavin Achtemeier Jul 30 '17 at 19:12
-
also here: https://stackoverflow.com/questions/16986658/python-call-external-program-non-blocking-way – Gavin Achtemeier Jul 30 '17 at 19:13
1 Answers
4
Hi I make this script for you using threading and subprocess to run other python script in the background (without the secondary script interrupting the first)
import threading
from subprocess import call
def thread_second():
call(["python", "secondscript.py"])
processThread = threading.Thread(target=thread_second) # <- note extra ','
processThread.start()
print 'the file is run in the background'
-
this is great but is there any way to stop the secondary script from outputting to the console? – invisabuble Jul 30 '17 at 18:59
-
@invisabuble that's a whole separate question which you should either lookup or ask. LMGTFY: https://stackoverflow.com/questions/2125702/how-to-suppress-console-output-in-python#25061573 , https://docs.python.org/3.5/library/subprocess.html#subprocess.call shows you that there is a kwarg for call that routes your stdout and stderr – Gavin Achtemeier Jul 30 '17 at 19:11
-
hello , you can use that it's helpful https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python – Jul 30 '17 at 20:15