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'])
Related
This question already has an answer here:
Python subprocess and user interaction
(1 answer)
Closed 2 years ago.
I have a program that when I run in shell, it asks for input from the user.
When I run it as follows with subprocess.Popen, it does not get into the part of the program
where it asks for input and it just finishes. How could I change that?
cmd = ["./pairwise", "-seq", sites, "-loc", locs, "-lk", "species_genotypenew_lk.txt", "-prefix", ldhat_out]
subprocess.Popen(cmd)
Thanks!
if pairwise is reads from stdin and writes to stdout
subprocess.Popen(cmd).communicate()
should do it.
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 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()
This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 9 years ago.
I tried these two methods:
os.system("python test.py")
subprocess.Popen("python test.py", shell=True)
Both approaches need to wait until test.py finishes which blocks main process. I know "nohup" can do the job. Is there a Python way to launch test.py or any other shell scripts and leave it running in background?
Suppose test.py is like this:
for i in range(0, 1000000):
print i
Both os.system() or subprocess.Popen() will block main program until 1000000 lines of output displayed. What I want is let test.py runs silently and display main program output only. Main program may quie while test.py is still running.
subprocess.Popen(["python", "test.py"]) should work.
Note that the job might still die when your main script exits. In this case, try subprocess.Popen(["nohup", "python", "test.py"])
os.spawnlp(os.P_NOWAIT, "path_to_test.py", "test.py")