Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to communicate with Queue in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to communicate in python Queue related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will have a harvest after reading this python Queue how to communicate article, let's take a look at it.

description

Queue can use multiprocessing module to realize data transmission between multiple processes.

Queue itself is a message queue program.

When the Queue() object is initialized (for example, q=Queue()), if the number of messages that can be received is not specified in parentheses, or if the number is negative, it means that there is no upper limit on the number of messages that can be received until the end of memory.

examples

from multiprocessing import Queue def queue_test(): q = Queue(3) #Initialize a Queue object that can receive up to three put messages q.put("Message 1") q.put("Message 2") print(q.full()) #False q.put("Message 3") print(q.full()) #True #Because the message queue is full, the following tries will throw an exception. The first try will wait 2 seconds before throwing an exception, and the second Try will throw an exception immediately. try: q.put("Message 4",True,2) except: print("Message queue full, number of existing messages:%s"%q.qsize()) try: q.put_nowait("Message 4") except: print("Message queue full, number of existing messages:%s"%q.qsize()) #Recommended way, first determine whether the message queue is full, and then write if not q.full(): q.put_nowait("Message 4") #When reading messages, first determine whether the message queue is empty, and then read if not q.empty(): for i in range(q.qsize()): print(q.get_nowait()) def main(): queue_test() if __name__= "__main__"main() About "Python Queue how to communicate" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "Queue in python how to communicate." If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report