map_queue_template.py 497 Bytes
Newer Older
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
import numpy as np
import os

from multiprocessing import Pool

import Queue
import multiprocessing

zz=20

q=multiprocessing.Queue()

for i in np.arange(30):
  q.put(i)

def f(x):
18
    global zz
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
19
20
21
22
23
24
25
26
27
28
    try:
      a=q.get() # get one more element
      zz = zz+1
      print x*x , os.getpid() , x, zz, " >> a= ",a
      return x*x
    except Queue.Empty:
      print "queue empty"

if __name__ == '__main__':
    p = Pool(5)
29
30

    a=list(p.map(f,np.arange(30) ))
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
31
32
    print ">>>",a
    print "zz=",zz