
Concurrent Computations on Multicore Processors
Multiprocessing API
Interprocess communication
Queues
For several processes we can use a Queue:
1 from m u lti proc essi ng import Process , Queue
2
3 def place _work ( q ):
4 q. put ( range (10))
5
6 def proc ess_ w ork ( q ):
7 data = q. get ()
8 q. put ([ n **3 for n in data ])
9
10 if __ n ame__ == ’ __main__ ’:
11 queue = Queue ()
12 p1 = Process ( t a r g e t = place_work , args =( queue ,))
13 p2 = Process ( t a r g e t = process_work , args =( queue ,))
14 p1 . start (); p2 . start (); p1 . join (); p2 . join ()
15 print ( queue . get ())
16 # Output : [0 , 1, 8, 27 , 64, 125 , 216 , 343 , 512 , 729]
Christian Zielinski 23