-1

I am trying to run a second python script from the first one. The second script is to run only when the conditions are met.

here is my poor code:

if T == 1 or O == 24 or V == 8 or Z == 45:
        subprocess.call([r'app_virtual_helper.bat'])

(the batch file is opening second python script)

everything worked well but when new script is running, the first one stops working.

How can I open second script when if is true and still have first script running?

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
JBTG
  • 91
  • 1
  • 7
  • Please have a look here: [click](https://stackoverflow.com/questions/10965949/can-subprocess-call-be-invoked-without-waiting-for-process-to-finish) – Muhammed Yücel Feb 01 '22 at 12:39

3 Answers3

2

Try this:

process = subprocess.Popen([r'app_virtual_helper.bat'])

So that your script doesn't wait for the call on your second script to end

jsa
  • 397
  • 2
  • 14
  • Want to explain your answer with links: The [`subprocess.Popen`](https://docs.python.org/3/library/subprocess.html#subprocess.Popen) executes a child program in a new process. See also [related question](https://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python). – hc_dev Feb 01 '22 at 12:47
1

Without threading:

Content of script1.py:

subprocess.call("python script2.py")

Output:

>python script1.py
Starting script1 function..
Script1 running.. 0
Script1 running.. 1
Script1 running.. 2
Script1 running.. 3
Launching second script..
Script2 running.. 0
Script2 running.. 1
Script2 running.. 2
Script2 running.. 3
Running script1 function again..
Script1 running.. 0
Script1 running.. 1
Script1 running.. 2
Script1 running.. 3

With threading:

Content of script1.py:

import time
import subprocess
import threading

def run():
    for i in range(4):
        print("Script1 running..", i)
        time.sleep(1)

print("Starting script1 function..")
run()
print("Launching second script..")
t = threading.Thread(target=subprocess.call, args=("python script2.py", True))
t.start()
print("Running script1 function again..")
run()

Output:

>python script1.py
Starting script1 function..
Script1 running.. 0
Script1 running.. 1
Script1 running.. 2
Script1 running.. 3
Launching second script..
Running script1 function again..
Script1 running.. 0
Script2 running.. 0
Script1 running.. 1
Script2 running.. 1
Script1 running.. 2
Script2 running.. 2
Script1 running.. 3
Script2 running.. 3

Content of script2.py:

import time

def run():
    for i in range(4):
        print("Script2 running..", i)
        time.sleep(1)

run()

If you rely on returns from your second script in your first script it makes things a little bit more difficult but is possible. This gives you an advantage of the previously mentioned subprocess.Popen() method. Hope this helps to get you started.

mnikley
  • 1,625
  • 1
  • 8
  • 21
  • 1
    Can you explain a bit, e.g. why `shell=True`? See the [docs](https://docs.python.org/3/library/subprocess.html#subprocess.Popen): "The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable." – hc_dev Feb 01 '22 at 13:11
  • @hc_dev thanks for elaborating. To be honest, i did not give it alot of thought - removed the argument from the code example. – mnikley Feb 01 '22 at 13:23
0

This code runs the 2nd file in a separate thread so the first scrips keeps running and does not stop when you call the 2nd file

from threading import thread

if T == 1 or O == 24 or V == 8 or Z == 45:

   test = Thread(target=call, args=(r'app_virtual_helper.bat'))
   test.start()
  • Why threading may be slower than subprocess: See [related question](https://stackoverflow.com/questions/6422187/why-thread-is-slower-than-subprocess-when-should-i-use-subprocess-in-place-of). – hc_dev Feb 01 '22 at 12:50