Martin
Martin

Reputation: 3385

Run python script from python script BUT outside of python script

It sounds like riddle or joke but actually I havent found answer to this problem.

What is actually the problem?

I want to run 2 scripts. In first script I call another script but I want them to continue parallely and not in 2 separate threads. Mainly I dont want 2nd script to be running inside 1st python script(That means if I run Chrome Browser from python script and then shut down the python script, the Chrome will be shut down too).

What I want is like on Linux machine: I open two terminals and run both scripts in each terminal - They are not two threads, they are independent on each other, shutting one will NOT shut down the other. Or it can be like on Linux machine where I can run 2 python scripts in terminal behind background with 'python xxx.py &' (&) symbol.

Summary:

I would like to run inside 'FIRST.py' script 'SECOND.py' script. However not with threading module and mainly have SECOND.py script independent on FIRST.py script, that is, shutting down FIRST.py will not have any consequence on SECOND.py. THE SOLUTION SHOULD BE WORKING ON WINDOWS, LINUX AND MAC.

BTW: I tried on windows:

subprocess.call(['python','second.py','&'])
subprocess.call(['python','second.py'])
os.system('python second.py') # I was desperate

I havent try Threading with daemon=False but I feel its kind of Demon and I dont feel my skill is that far that I can control threads existing outside of my playground :)

Thanks in advance for help

Upvotes: 1

Views: 181

Answers (1)

Kai Guther
Kai Guther

Reputation: 793

You can use the Popen constructor from the subprocess module to launch background processes, using

import subprocess
p = subprocess.Popen(["python","second.py"])

creates a background process and execution of first.py is not blocked.

Upvotes: 1

Related Questions