I tried to read the documentation at http://docs.python.org/dev/library/multiprocessing.html but I'm still struggling with multiprocessing Queue, Pool and Locking. And for now I was able to build the example below.
Regarding Queue and Pool, I'm not sure if I understood the concept in the right way, so correct me if I'm wrong. What I'm trying to achieve is to process 2 requests at time ( data list have 8 in this example ) so, what should I use? Pool to create 2 processes that can handle two different queues ( 2 at max ) or should I just use Queue to process 2 inputs each time? The lock would be to print the outputs correctly.
import multiprocessing
import time
data = (['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)
def mp_handler(var1):
for indata in var1:
p = multiprocessing.Process(target=mp_worker, args=(indata[0], indata[1]))
p.start()
def mp_worker(inputs, the_time):
print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
if __name__ == '__main__':
mp_handler(data)
Here is an example from my code (for threaded pool, but just change class name and you'll have process pool):
Basically:
pool = ThreadPoolExecutor(6)
creates a pool for 6 threadspool.submit(execute_run, rp)
adds a task to pool, first arogument is a function called in in a thread/process, rest of the arguments are passed to the called function.pool.join
waits until all tasks are done.Here is my personal goto for this topic:
Gist here, (pull requests welcome!): https://gist.github.com/thorsummoner/b5b1dfcff7e7fdd334ec
The best solution for your problem is to utilize a
Pool
. UsingQueue
s and having a separate "queue feeding" functionality is probably overkill.Here's a slightly rearranged version of your program, this time with only 2 processes coralled in a
Pool
. I believe it's the easiest way to go, with minimal changes to original code:Note that
mp_worker()
function now accepts a single argument (a tuple of the two previous arguments) because themap()
function chunks up your input data into sublists, each sublist given as a single argument to your worker function.Output:
Edit as per @Thales comment below:
If you want "a lock for each pool limit" so that your processes run in tandem pairs, ala:
A waiting B waiting | A done , B done | C waiting , D waiting | C done, D done | ...
then change the handler function to launch pools (of 2 processes) for each pair of data:
Now your output is:
For everyone using editors like Komodo Edit (win10) add
sys.stdout.flush()
to:or as first line to:
This helps to see what goes on during the run of the script; in stead of having to look at the black command line box.
This might be not 100% related to the question, but on my search for an example of using multiprocessing with a queue this shows up first on google.
This is a basic example class that you can instantiate and put items in a queue and can wait until queue is finished. That's all I needed.