Python inter-process communication queue

Source: Internet
Author: User

    • Use of queue

Queue.qsize () #返回当前队列包含的消息数量

Queue.empty () #如果队列为空, returns True, and vice versa false
Queue.full () #如果队列满了, returns True, and vice versa false

Queue.get ([block[, timeout]]) #获取队列中的一条消息, and then remove it from the queue, the block defaults to true, not taken to block

#如果block值为False, if the message queue is empty, an "Queue.empty" exception is thrown immediately

Queue.get_nowait () #相当Queue. Get (False)

Queue.put (item,[block[, timeout]]) #将item消息写入队列, the block default value is True,

#消息列队如果已经没有空间可写入, the program will be blocked (stopped in write state) until space is freed from the message queue

#如果block值为False, Message Queuing throws a "Queue.full" exception immediately if there is no space to write

Queue.put_nowait (item) #相当Queue. Put (item, False)

    • Instance

Process

    

 fromMultiprocessingImportProcess, QueueImportOS, time, Random#code to write the data Process execution:defWrite (q): forValueinch['A','B','C']:        Print('Put%s to queue ...'%value) q.put (value) time.sleep (Random.random ())#read the code that the data process executes:defRead (q): whileTrue:if  notq.empty (): Value=q.get (True)Print('Get%s from queue.'%value) Time.sleep (Random.random ())Else:             Breakif __name__=='__main__':    #The parent process creates a queue and passes it to each child process:Q =Queue () PW= Process (Target=write, args=(q,)) PR= Process (Target=read, args=(q,))#Start child process PW, write:Pw.start ()#wait for PW to end:Pw.join ()#start child process PR, read:Pr.start () pr.join ( )#PR process is a dead loop, can not wait for its end, can only forcibly terminate:    Print('all data is written and read out')

Pool

#Coding=utf-8#Modify the queue in import to manager fromMultiprocessingImportManager,poolImportOs,time,randomdefReader (q):Print("Reader Startup (%s), parent process is (%s)"%(Os.getpid (), Os.getppid ())) forIinchRange (Q.qsize ()):Print("reader gets the message from the queue:%s"%Q.get (True))defwriter (q):Print("writer Start (%s), parent process is (%s)"%(Os.getpid (), Os.getppid ())) forIinch "Dongge": Q.put (i)if __name__=="__main__":    Print("(%s) Start"%os.getpid ()) Q=manager (). Queue ()#initialize using a queue in Managerpo=Pool ()#use blocking mode to create a process, so that you do not need to use the dead loop in reader, you can let writer complete execution, and then use reader to readPo.apply (writer, (Q,)) po.apply (reader, (q,)) Po.close () Po.join ()Print("(%s) End"%os.getpid ())

Python inter-process communication queue

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.