I'm trying to run a couple of calculations concurrently based on this example. I extended it in the way that instead of just a simple function, I'm running some external software via subprocess. It's run with Python 2.7.6 on Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic x86_64).
There is a Queue involved to keep track of result outputs. This Queue is supposed to get filled, but appears to be empty after calculations are done.
The simplified code looks like this:
import subprocess, shlex, os, random, pickle
from Queue import Queue
from multiprocessing import Process
from time import sleep
def mp_solve(problems, nprocs):
def worker(problem, out_q):
outdict = []
cmd = "..." + problem
args = shlex.split(cmd)
output,error = subprocess.Popen(args,stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()
outdict = [str(problem), str(output)]
out_q.put(outdict)
print out_q.empty() #prints False
out_q = Queue() #create Queue
procs = []
for i in range(nprocs):
p = Process(
target=worker,
args=(problems[i][1], out_q))
procs.append(p)
p.start()
sleep(10) #calculations are limited to 3 seconds through a parameter passed to external program
print out_q.empty() #prints True
resultlist = []
for i in range(nprocs):
print "going to Q" + str(i)
try:
resultlist.append(out_q.get())
except Queue.Empty:
print "Queue empty"
mp_solve(list_of_problems, 10)
The output of this will be
False
False
False
False
False
False
False
False
False
False
True
going to Q0
After the last command, the session window is rendered useless. I can type in it, but nothing will happen and even Ctrl + C has no effect. I then just close the ssh session.
I'm fairly new to multiprocessing and I can't figure out why the Queue appears to get filled correctly (as seen from the False returned) but is then empty. Note that Queue.Empty never seems to catch. Any ideas how to get me back on the right track?