tags:

views:

169

answers:

3

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

+5  A: 

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.

jldupont
but How will I know when all three have finished? I want the rest of the code to execute only when the commands finish running
TP
@Jaelebi: see my updated answer.
jldupont
@Jaelebi: `status = [ p.wait() for p in list_of_popen_objects ]` usually works to see if all processes are done.
S.Lott
A: 
os.system("query.pl -results '10000' -serverName 'server1' &") 
os.system("query.pl -results '10000' -serverName 'server2' &") 
os.system("query.pl -results '10000' -serverName 'server3' &")

in this case - process will be started in background

Oduvan
can't really know what's happening with this sort of "solution"...
jldupont
Oduvan
A: 

You can use Queue

tasks = ("query.pl -results '10000' -serverName 'server1'",\
"query.pl -results '10000' -serverName 'server2'",\
"query.pl -results '10000' -serverName 'server1'")

def worker():
    while True:
        item = q.get()
        os.system(item)

q = Queue()
for i in tasks:
     t = Thread(target=worker)
     t.setDaemon(True)
     t.start()

for item in tasks:
    q.put(item)

q.join()
Oduvan
Using threads in this way isn't really a good idea when clean and predictable asynchronous IO facilities are available. Additionally, spawning processes and threads don't mix well on some platforms.
mch