This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 9 years ago.
What command in Python can be used to run another Python program?
It should not wait for the child process to terminate. Instead, it should continue on. It also does not need to remember its child processes.
Use subprocess:
import subprocess
#code
prog = subprocess.Popen(['python', filename, args])
#more code
If the other Python program is importable, and the functionality you need can be called via a function, then it is preferable to use multiprocessing instead of subprocess, since the arguments can be passed as Python objects, instead of via strings:
import somescript
import multiprocessing as mp
proc = mp.Process(target=somescript.main, args=...)
proc.start()
Related
This question already has answers here:
Launch a totally independent process from Python
(2 answers)
Closed 1 year ago.
When i try to run something like
os.system('start')
or
os.system('start file.txt')
or
os.startfile('file.txt')
it does start console, notepad or whatever, but when my python program executes, those opened programs close as well.
So my question is: Is there a way to do things like above, but without closing those programs along with my script?
It will work with frunction from the subprocess module.
Here is an example with Popen :
from subprocess import Popen
p = Popen(['notepad.exe'])
You can also use call
import subprocess
subprocess.call(['calc.exe'])
This question already has answers here:
Run a process and kill it if it doesn't end within one hour
(5 answers)
Using module 'subprocess' with timeout
(31 answers)
Closed 4 years ago.
I am making a new script(p.py)for my Raspberry Pi and i am trying to combine 2 python scripts named Final.py and A.py . I want to execute the Final.py for about 5 seconds and then kill it and proceet to A.py, run it for 25 seconds and kill it and repeat the whole process endlessly. However, both of the scripts are looped and i don't know how to end these processes. Here the p.py code:
import subprocess
import time
execfile('Finaaal.py') #looped script 1
time.sleep(5.0)
subprocess.call(['./1.sh']) #this kills Finaaal.py
execfile('A.py') #looped script 2
time.sleep(25.0)
subprocess.call(['./2.sh']) #this kills A.
Does anyone know any better ideas or a better way to fix this? Everything would be greatly appreciated!
What you're trying to do doesn't really make any sense. You run the sub-scripts like this:
execfile('Finaaal.py') #looped script 1
But execfile just runs that script in the current interpreter, not as a separate program. Which means there's no way script 1.sh could possibly kill Finaaal.py without also killing the controller script.
The answer is simple—you've already imported subprocess, you just need to use it to run the sub-scripts instead of using execfile. And then you don't even need 1.sh and 2.sh scripts; you can just kill the processes directly:
import subprocess
import sys
import time
p = subprocess.Popen([sys.executable, 'Finaaal.py'])
time.sleep(5.0)
p.kill()
p = subprocess.Popen([sys.executable, 'A.py'])
time.sleep(5.0)
p.kill()
Since you've tagged your question both python-2.7 and python3.x, you probably need to think about which Python should be used to run the sub-scripts. Using sys.executable means it'll be the same one used to run the controller script, which is usually what you want (but if not, obviously do something different there, whether that's "hardcode python3" or "whatever the shbang says" or whatever's appropriate).
(Since you appear to be on POSIX, you can even modify Finaaal.py and A.py to catch the SIGKILL and do some clean shutdown if you want. Or, if you want a hard shutdown instead of having that option, use terminate instead.)
This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 7 years ago.
I have a python script. Which is running as a service with a while loop for ever. The script need to be executed by another python but without waiting for the out put it should pass through.
So the main script with while loop is as follows "main.py". Which never going to be end.
while True:
# do some task
time.sleep(5)
This need to be executed by another python "start.py" with similar function as follows.
os.system("main.py 1")
OR
subprocess.Popen("python main.py")
Problem here "start.py" won't finish as witing for the out put of "main.py". But I want to make it like "start.py" need to load "main.py" and leave it in background. Then the "start.py" need to complete the process. How can I modify the
os.system("main.py 1")
function to skip the waiting for "main.py"? Please consider this need to run on cross platform.
I recommend for you to check out Plumbum https://plumbum.readthedocs.org/en/latest/ specifically the section on background/foreground https://plumbum.readthedocs.org/en/latest/#foreground-and-background-execution
from plumbum import BG
from plumbum.cmd import python
python('main.py', '1') & BG
Cross platform also.
This question already has answers here:
Why is subprocess.Popen not waiting until the child process terminates?
(3 answers)
Closed 8 years ago.
I have a python programme as below
import os
import subprocess
for m in range(0,10):
os.chdir("C:/")
run="my command%d"%m
subprocess.Popen(run).wait()
Where 'my command' is something I used to launch another programme.
Although I have wait() after Popen, it turns out that the 10 programmes still run simultaneously, not as expected.
How do I settle this issue?
Two options:
use subprocess.check_call() (which should run sequentially)
use Popen.communicate() ( https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate) with stdout and stderr set to subprocess.PIPE to see if stdout and stderr are indeed sequentially generated
Also, datetime.datetime.now() gives you a microsecond, so you can see time with granularity higher than 1s.
This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 7 years ago.
I'm writing an application in python that initiates a JVM in java by calling a shell script using a python subprocess. However, my problem is that the correct way I have it written, the JVM starts and blocks the rest of the processes that happen after it. I need the JVM to run while I'm calling another function, and I need to stop the JVM after the process has finished running.
Python code:
process = subprocess.Popen('runJVM.sh', shell=True, stderr=subprocess.STDOUT)
process.wait()
r = Recommender()
r.load()
assert len(sys.argv) > 1, '%d arguments supplied, one needed' %(len(sys.argv)-1)
print "recommendations" + str(r.get_users_recommendation(sys.argv[1:]))
....
def get_users_recommendation(self, user_list):
j_id_list = ListConverter().convert(class_list, self.gateway._gateway_client)
recs = self.gateway.entry_point.recommend(j_id_list)
return recs
Where:
from py4j.java_gateway import JavaGateway
self.gateway = JavaGateway()
I can't get the get_user_recommendations to run because the JVM server is blocking the process. How do I not make it block the rest of the Python script and then kill it once the python methods have finished running and returned a value? Much thanks.
process.wait() is blocking your process. Remove that line and the rest of the code will run.
Then you can make it end by calling Popen.terminate()
Popen is invoking CreateProcess, that means the OS launches a new process and returns the PID to be stored on your process variable.
If you use process.wait(), then your code will wait until the recently created process ends (it never ends in this case unless you terminate it externally).
If you do not call process.wait(), your code will continue to execute. But you still have control over the other process and you can do things like process.terminate() or even process.kill() if necessary.