Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple commands simultaneously from python

Tags:

python

I want to run three commands at the same time from python. The command format is query.pl -args

Currently I am doing

os.system("query.pl -results '10000' -serverName 'server1' >> log1.txt")

os.system("query.pl -results '10000' -serverName 'server2' >> log2.txt")

os.system("query.pl -results '10000' -serverName 'server3' >> log3.txt")

I want to query all three servers at the same time but in this case, each command executes only after the last one has finished. How can I make them simultaneous? I was thinking of using '&' at the end but I want the next part of the code to be run only when all three command finish

like image 685
randomThought Avatar asked Dec 29 '22 12:12

randomThought


1 Answers

You could use the subprocess module and have all three running independently: use subprocess.Popen. Take care in setting the "shell" parameter correctly.

Use the wait() or poll() method to determine when the subprocesses are finished.

like image 100
jldupont Avatar answered Jan 01 '23 02:01

jldupont