File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from multiprocessing import Process
2+ import os
3+
4+ def info (title ):
5+ print (title )
6+ print ('module name:' , __name__ )
7+ print ("parent process:" , os .getppid ())
8+ print ("process id:" , os .getpid ())
9+
10+ def f (name ):
11+ info ('function f' )
12+ print ('hello' , name )
13+
14+ if __name__ == '__main__' :
15+ info ('main line' )
16+ p = Process (target = f , args = ('bob' ,))
17+ p .start ()
18+ p .join ()
Original file line number Diff line number Diff line change 1+ import multiprocessing
2+ from time import ctime
3+
4+ def consumer (input_q ):
5+ print ("Into consumer:" , ctime ())
6+ while True :
7+ item = input_q .get ()
8+ if item is None :
9+ break
10+ print ("pull" , item , "out of q" )
11+ input_q .task_done ()
12+ print ("Out of consumer:" , ctime ())
13+
14+ def producer (sequence , output_q ):
15+ print ("into producer:" , ctime ())
16+ for item in sequence :
17+ output_q .put (item )
18+ print ("put" , item , "into q" )
19+ print ("out of producer:" , ctime ())
20+
21+ if __name__ == '__main__' :
22+ q = multiprocessing .JoinableQueue ()
23+ cons_p = multiprocessing .Process (target = consumer , args = (q ,))
24+ cons_p .daemon = True
25+ cons_p .start ()
26+ cons_p_2 = multiprocessing .Process (target = consumer , args = (q ,))
27+ cons_p_2 .start ()
28+
29+ sequence = [1 ,2 ,3 ,4 ]
30+ producer (sequence , q )
31+ q .put (None )
32+ q .put (None )
33+ q .join ()
You can’t perform that action at this time.
0 commit comments