Python Message Queuing queue

Source: Internet
Author: User

Example 1: Message Queuing queue, do not name the file "queue.py", otherwise it will report the exception "Importerror:cannot Import name ' queue '"

#coding =utf-8from multiprocessing Import Queue q = Queue (3) #初始化一个Queue对象, you can receive up to three put messages Q.put (' message-1 ') q.put (' Message-2 ') print (Q.full ()) #False, is full of q.put (' message-3 ') print (Q.full ()) #True # Because the message queue is full, the following try throws an exception, The first try waits 2 seconds before throwing an exception, and the second try throws an exception immediately try:q.put (' message-4 ', true,2) except:print (' EXCEPT1, message queue full, number of existing messages:%s '%q.qsize ()) Try:q.put_nowait (' message-4 ') except:print (' Except2, message queue full, number of existing messages:%s '%q.qsize ()) #判断队列是否已满if not Q.full (): Q.put_ NoWait (' message-4 ') #读取消息时, first determine whether the message queue is empty, read if not Q.empty (): For I in range (Q.qsize ()):p rint (Q.get ()) #q. Get will block, q.get_ NoWait () does not block, but throws an exception

False

True

EXCEPT1, Message queue full, number of existing messages: 3

EXCEPT2, Message queue full, number of existing messages: 3

Message-1

Message-2

Message-3


Example two: interprocess communication through process

From multiprocessing import Process,queueimport os,time,random #写数据def write (q): For value in [' A ', ' B ', ' C ']:p rint (' Put%s To queue ... '%value) q.put (value) time.sleep (Random.random ()) #读数据def read (q): While True:if not Q.empty (): value = Q.get ( True) print (' Get%s from queue ... '%value) time.sleep (Random.random ()) else:breakif __name__ = = ' __main__ ':p rint (' Start ... ') q = Queue () #父进程的queue传递给子进程pw = Process (target=write,args= (q,)) PR = Process (target=read,args= (q,)) #写进程pw. Start ( ) Pw.join () #读进程pr. Start () Pr.join () print (' Done ... ')

Start ...

Put A to queue ...

Put B to queue ...

Put C to queue ...

Get A from Queue ...

Get B from Queue ...

Get C from Queue ...

Done ...


Example three: inter-process communication through manager

From multiprocessing import Manager,poolimport os,time,random #写数据def writer (q):p rint (' writer start (%s), parent process is (%s) '% ( Os.getpid (), Os.getppid ())))) for I in ' Chaoge ': q.put (i) #读数据def reader (q):p rint (' Reader startup (%s), parent process is (%s) '% (Os.getpid (), Os.getppid ()))) for I in Range (Q.qsize ()):p rint (' Reader fetches from queue to message:%s '%q.get ()) If __name__ = = ' __main__ ':p rint (' (%s) Start '%os.getpid ()) q = Manager (). Queue () #使用Manager中的Queue来初始化po =pool () #使用阻塞模式创建进程 so that you do not need to use a dead loop in reader, you can wait for the write execution to finish, then use Readerpo.apply (writer, (q ,)) po.apply (reader, (q,)) #写进程po. Close () Po.join () print (' (%s) End '%os.getpid ())

(7720) Start

Writer Start (7284), parent process is (7720)

Reader Startup (8712), parent process is (7720)

Reader gets the message from the queue: C

Reader gets the message from the queue: H

Reader gets the message from the queue: a

Reader gets the message from the queue: O

Reader gets the message from the queue: g

Reader gets the message from the queue: E

(7720) End


Python Message Queuing 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.