Python Forum
Run multiple process using subprocess
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run multiple process using subprocess
#1
How do I run my script proces1.py and process2.py into a single script to check if both the process are running and if so kill and restart the process again(I am passing arguments seperately in both the script need to achieve this from one script)
#Process1.py
for process in psutil.process_iter():
if (process.name().startswith('python3')) and ("new_scene" in process.cmdline()[1]):
    print('Process found')
    process.kill()
   time.sleep(5)
cmd='python3  /Desktop/Test/Script/new/new_scene.py --run  /Desktop/Test/Script/new/scenario/examples'
subprocess.call(cmd + sys.argv[1],shell=True)
break
else:
print('Process not found')
#Process2.py
for process in psutil.process_iter():
if (process.name().startswith('python3')) and ("Demo" in process.cmdline()[1]):
    print('Process found')
    process.kill()
   time.sleep(5)
cmd='python3  /Desktop/Test/Script/new/Demo.py --run  /Desktop/Test/Script/new/scenario/examples'
subprocess.call(cmd + sys.argv[1],shell=True)
break
else:
print('Process not found')
Reply
#2
(Nov-24-2021, 02:23 PM)Shiri Wrote: How do I run my script proces1.py and process2.py into a single script to check if both the process are running and if so kill and restart the process again(I am passing arguments seperately in both the script need to achieve this from one script)

From the link I shared, it is clear that if you want to run them in parallel you should go for 'Popen' while otherwise, you should go for 'subprocess.call'

cmd1 = "<pathtoscript1>"
cmd2 = "<pathtoscript2>"

process1 = Popen(cmd1, shell=True)
process2 = Popen(cmd2, shell=True)
Not something I have done before, but, yeah this looks correct.
Reply
#3
This part I got Thanks , but my doubt is in the if condition where both the scripts use different process name (process1.py searches for the process name and kill it how can i achieve this for process2.py in same script)
Reply
#4
(Nov-24-2021, 08:19 PM)Shiri Wrote: This part I got Thanks , but my doubt is in the if condition where both the scripts use different process name (process1.py searches for the process name and kill it how can i achieve this for process2.py in same script)

Combining the scripts just as is should be good even after which you could probably factor out repeated code into a function.

That would be good enough from me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Process multiple pdf files Spartan314 1 306 Oct-27-2021, 10:46 PM
Last Post: Larz60+
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 1,020 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Defining multiple functions in the same def process sparkt 5 1,269 Aug-09-2020, 06:19 PM
Last Post: sparkt
  How to sharing object between multiple process from main process using Pipe Subrata 1 2,102 Sep-03-2019, 09:49 PM
Last Post: woooee
  Breaking subprocess loop from parent process kapibara 3 1,996 Aug-28-2019, 11:34 PM
Last Post: kapibara
  Multiple process access to a serial port mkonnov 0 1,622 Apr-14-2019, 12:42 PM
Last Post: mkonnov
  decoding sub.process output with multiple \n? searching1 2 1,555 Feb-24-2019, 12:00 AM
Last Post: searching1
  Good way to have a worker queue accessible by multiple process? cheater 2 1,571 Dec-21-2017, 09:30 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020