Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run & stop python script from another python script?

Tags:

python

I want code like this:

if True:
    run('ABC.PY')
else:
    if ScriptRunning('ABC.PY):
       stop('ABC.PY')
    run('ABC.PY'):

Basically, I want to run a file, let's say abc.py, and based on some conditions. I want to stop it, and run it again from another python script. Is it possible?

I am using Windows.

like image 716
John Bott Avatar asked Jan 26 '23 16:01

John Bott


1 Answers

You can use python Popen objects for running processes in a child process

So run('ABC.PY') would be p = Popen("python 'ABC.PY'")

if ScriptRunning('ABC.PY) would be if p.poll() == None

stop('ABC.PY') would be p.kill()

like image 148
rok Avatar answered Feb 26 '23 19:02

rok