Running multiple Python scripts simultaneously and then sequentially

白昼怎懂夜的黑 提交于 2020-08-21 19:35:42

问题


I can run multiple Python scripts simultaneously from a bash script like this;

#!/bin/bash
python pr1.py & 
python pr2.py &
python aop.py &
python loader.py &

But what if I want a batch to fire simultaneously and after they've run, start some more sequentially. Will this work?:

#!/bin/bash
python pr1.py & 
python pr2.py &
python ap.py &
python loader.py
python cain.py
python able.py

回答1:


On your bash script you can simply add the wait command like this:

#!/bin/bash
python pr1.py & 
python pr2.py &
python ap.py &
wait
python loader.py
python cain.py
python able.py

wait will, obviously, wait for all the jobs (the background proccess you fired) to be finished for it to continue.




回答2:


Once you put & at the end, it runs as a background process. Hence all the scripts ending with & run in parallel.

To run the other 3 scripts in sequential order you can try both:

&& runs the next script only if the preceding script has run successfully

python loader.py && python cain.py && python able.py 

|| runs scripts sequentially irrespective of the result of preceding script

python loader.py || python cain.py || python able.py



回答3:


With the & command you are running the scripts in the background. you could add a check in a loop to run the command jobs and see if it continues to return a list of jobs. when it stops you can continue with your next batch of python calls.




回答4:


Why not try it out ?

#1.py
import time
time.sleep(3)
print("First script")

#2.py
import time
time.sleep(3)
print("Second script")

If you put the processes into background, you will see the output from both the python scripts at the same time.

#!/bin/bash
python 1.py &
python 2.py &

If you execute it without the &, then you will see the output from the second script after 6 seconds.

#!/bin/bash
python 1.py
python 2.py

PS: Be careful to take care of dependencies and concurrent access issues while running it in parallel



来源:https://stackoverflow.com/questions/42072715/running-multiple-python-scripts-simultaneously-and-then-sequentially

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!