Very similar to open source software MQ
FIFO logic, one, n A, 1, n listen q
From multiprocessing import Process,queue
#队列, FIFO
Q=queue (3)
Q.put ({' A ': 1})
Q.put (' B ')
Q.put (' C ')
Print (Q.full ())--Full
Q.put (' d ', False) #等同于q. put_nowait (' d ') full, no wait, direct error
Q.put (' d ', timeout=2) #塞满后 wait 2 seconds, can not be error
Print (Q.qsize ()) #查看q中 number of values
Print (Q.get ())
Print (Q.get ())
Print (Q.get ())
Print (Q.empty ())--and whether to take empty
Print (Q.get (block=false)) #等同于q. get_nowait (' d ') listen and listen, no wait, direct error
Print (q.get_nowait ())
Print (Q.get (timeout=2)) #听满后 listen to wait 2 seconds, can not hear the error
========================== Simulation Real Case ======================
Eat buns, cook, customers eat, do not interfere with each other
def GET (Q,name,li):
Print (' customer ', name)
For line in Li:
Time.sleep (1)
Print (' Customer ate ', Q.get ())
def PUT (Q,name,li):
Print (' Chef ', name)
For line in Li:
Time.sleep (1)
Print ('%s produced%s '% (name,line))
Q.put (line)
if __name__ = = ' __main__ ':
Q=queue ()
Li = [' bun%s '% i for I in range (10)]
P=process (target=put,args= (q, ' La ', Li))
P.start ()
P1=process (target=get,args= (q, ' Onda ', Li))
P1.start ()
But the problem is, the customer does not release the process after eating, still stuck
=============================== submit a fixed value by put, let get to judge ======================
def GET (q,name):
While True:
Time.sleep (2)
Res=q.get ()
If Res is none:break
Print (' customer%s, ate%s '% (name,res))
def PUT (Q,name,li):
For line in Li:
Time.sleep (1)
Q.put (line)
Print (' Chef%s has only%s '% (name,line))
Q.put (None)
if __name__ = = ' __main__ ':
Q=queue ()
P1=process (target=get,args= (q, ' La '))
P1.start ()
li=[' bun%s '%i for I in range (10)]
P2=process (target=put,args= (q, ' Onda ', Li))
P2.start ()
P1.join ()
P2.join ()
Print (' main process ')
============================== or use joinablequeue===========
From multiprocessing import Process,joinablequeue
Import time
Import Random
DEF consumer (q,name):
While True:
# Time.sleep (Random.randint (1,3))
Res=q.get ()
Q.task_done ()
Print (' \033[41m consumer%s got%s\033[0m '% (name,res))
def producer (Seq,q,name):
For item in SEQ:
# Time.sleep (Random.randint (1,3))
Q.put (item)
Print (' \033[42m producer%s produced%s\033[0m '% (Name,item))
Q.join ()
Print (' ============>> ')
if __name__ = = ' __main__ ':
Q=joinablequeue ()
C=process (target=consumer,args= (q, ' Egon '),)
C.daemon=true #设置守护进程, the end of the main process is C.
C.start ()
seq=[' bun%s '%i for I in range (10)]
P=process (target=producer,args= (Seq,q, ' Chef 1 '))
P.start ()
# Master--->producer----->q--->consumer (10 times Task_done)
P.join () #主进程等待p结束, p waits for the C to finish the data, C once the data is taken, P.join is no longer blocked, into
# and the main process is finished, the master process is finished and the daemon C is recycled, and C is not necessary at this time.
Print (' main process ')
In Get ()
Q.task_done () Every one of them, he will report it until he finishes eating.
In put ()
Q.join (), wait for Get () to finish eating, put can get to, then go down,
While put () is still stuck in listening to Q
Get ()
So set the daemon to get (), release the Get after the main process is released
Main process:
When the main process waits for the put () request to end through P.join (), and the put () request is finished with Get (), the final master process ends, and then the Get () is released
Python-day9 queue