Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run another Python program without holding up original [duplicate]

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.

like image 405
PyRulez Avatar asked Jun 26 '13 16:06

PyRulez


2 Answers

Use subprocess:

import subprocess

#code
prog = subprocess.Popen(['python', filename, args])
#more code
like image 112
xgord Avatar answered Oct 31 '22 08:10

xgord


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()
like image 35
unutbu Avatar answered Oct 31 '22 10:10

unutbu