Python inter-process communication consumer producer Model queue

Source: Internet
Author: User

Queue from multiprocessing import queue

Queue: FIFO (first-in-a-out abbreviation)///stack: Advanced back-out (FILO)

Description of the Queue method:

= Queue ([maxsize]) Q. Get ([block [, timeout]]) returns an item in Q. If q is empty, this method blocks until an item in the queue is available. Block is used to control blocking behavior, which is true by default. If set to False, an Queue.empty exception is thrown (defined in the queue module). Timeout is an optional time-out that is used in blocking mode. If no items are available within the specified time interval, an Queue.empty exception is thrown. Q.get_nowait () with Q. Get (False) method. Q.put (item [, block [, timeout]]) puts the item in the queue. If the queue is full, this method will block until there is space available. Block controls blocking behavior, which defaults to true. If set to False, an Queue.empty exception is thrown (defined in the Queue Library module). Timeout Specifies the length of time to wait for free space in blocking mode. A Queue.full exception is thrown after the timeout. Q.qsize () returns the correct number of current items in the queue. The result of this function is unreliable because items may have been added or deleted in the queue between returning results and using the results later in the program. On some systems, this method may throw an Notimplementederror exception. Q.empty () returns TRUE if q is null when this method is called. If other processes or threads are adding items to the queue, the results are unreliable. That is, between returning and using the result, a new item may have been added to the queue. Q.full () returns True if Q is full. Due to the presence of threads, the results may also be unreliable (refer to the Q.empty () method): 

Other methods:

Q.close () closes the queue to prevent more data from being added to the queue. When this method is called, the background thread will continue to write to the data that has been queued but not yet written, but will close as soon as this method completes. If q is garbage collected, this method is called automatically. Closing a queue does not generate any type of data end signal or exception in the queue consumer. For example, if a consumer is being blocked on a get () operation, shutting down a queue in a producer does not cause the get () method to return an error. Q.cancel_join_thread () will no longer automatically connect to the background thread when the process exits. This will prevent the Join_thread () method from blocking. Q.join_thread () The background thread that connects the queue. This method is used to wait for all queue items to be consumed after the Q.close () method is called. By default, this method is called by all processes that are not the original creator of Q. Calling the Q.cancel_join_thread () method can prohibit this behavior.
 fromMultiprocessingImportQueueq= Queue (4) Q.put (1) Q.put (2) Q.put (3) Q.put (4)#Q.put (5) #如果是put because the data is already filled, so the program blocks the put and waits for the data to be fetched#q.put_nowait (5) # If it is put_nowait (), does not block, directly into the queue data, the queue full report exception#Try:#q.put_nowait (5) # The exception is handled directly here with a try. Data is not placed in the queue and is discarded directly#except:#print (' Queue full ')Print(Q.get ())Print(Q.get ())Print(Q.get ())Print(Q.get ())#print (Q.get ()) # here is the same as the top because the queue is empty, so the program blocks in get, waiting to put the data#print (Q.get_nowait ()) # does not block, fetches data directly from the queue, and does not get the errorTry: q.get_nowait ()except :    Print('queue is empty')

Queues for inter-process communication

 fromMultiprocessingImportProcess, QueueImport Timedeffunc (q): Time.sleep (1) Q.put ('I am a Chinese')if __name__=='__main__': Q= Queue (5) P= Process (target=func,args=(q,)) P.start ()#the child process runs concurrently with the parent process, not necessarily the data in the queue    Print(Q.get ())#must not error, get () is blocked to get the data if the queue is not waiting    #print (q.get_nowait ()) #数据为空是获取不到会报错
 fromMultiprocessingImportProcess,queueImportTime,os#Import RandomdefP_func (q): Q.put (Os.getpid ())defG_func (q):Print(Q.get ())if __name__=='__main__': Q= Queue (5) L_put=[] L_get= []     forIinchRange (10): P_put= Process (target=p_func,args=(q,)) P_put.start () l_put.append (p_put) forIinchRange (10): P_get= Process (Target=g_func, args=(q,)) P_get.start () l_get.append (P_get) [I.join () forIinchL_get] [I.join () forIinchL_put]

Producer Consumer Model

is mainly for decoupling
Using queues to implement producer consumer models

Python inter-process communication consumer producer Model queue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.