Gevent intercoroutine communication and queue and event usage

Source: Internet
Author: User

Preface:

Today, we will write the usage of advanced gevent points. For me, these are also common gevent modules.


The usage of the asyncresul module of gevent. In fact, the official statement is somewhat confusing. To put it bluntly, it is the communication between the coroutine. I am the boss and asked my eldest brother and younger brother's colleagues to collect their bills. After the younger brother completes, he will wait for the eldest brother to ask questions. If the younger brother is not finished and is still doing things, the eldest brother will wait for the younger brother to return results within a period of time. Keep waiting!


This is interesting in practice. We do one thing at the same time, but we need mutual help or communication. In this case, you can use asyncresult.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/56/wKiom1PnhUWzgzDyAALebVnD0sk717.jpg "Title =" 000.jpg" alt = "wkiom1pnhuwzgzdyaalebvnd0sk717.jpg"/>


# Coding: UTF-8 # http://rfyiamcool.blog.51cto.com/1030776/1538367import geventfrom gevent. Event import asyncresulta = asyncresult () def Xiaodi (): "" a moment of congestion for 10 seconds! "Print" Xiaodi starts "gevent. sleep (10). set ('Hello World') print "Xiaodi ended" def dage (): "You need to wait for Xiaodi to complete, he can I live "" Print 'dage. This is the first step... 'print. get () # blocking print 'I live! 'Gevent. joinall ([gevent. spawn (Xiaodi), gevent. spawn (dage),])


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/56/wKiom1PnhcfCgQR1AACraArCixU937.jpg "Title =" 1111.jpg" alt = "wkiom1pnhcfcgqr1aacraarcixu937.jpg"/>


Here is the usage of gevent events. Rawlink registers a callback, and wait waits until the task is completed before it can proceed. Its usage is somewhat like some of twisted's ideas, registration events, events, and callback function associations. The biggest advantage of gevent is to use synchronous writing to Implement Asynchronous functions. Therefore, this function is not practical.

E = event ()

E. rawlink (callback_def)

E. Wait ()

E. xxx


# Coding: UTF-8 # http://rfyiamcool.blog.51cto.com/1030776/1538367import geventimport timedef event_setter (e): print 'started to get involved... 'E. rawlink (event_callback) gevent. sleep (2) print '2017... 'E. set () def event_waiter (e): Print 'wait... 'E. wait () print 'Wait for end' def event_callback (e): Print "Callback... "def try_event (): From gevent. event import event E = event () gevent. joinall ([gevent. spawn (event_setter, e), gevent. spawn (event_waiter, e), gevent. spawn (event_waiter, e), gevent. spawn (event_waiter, e), gevent. spawn (event_waiter, e), gevent. spawn (event_waiter, e), gevent. spawn (event_waiter, e),] try_event ()


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/57/wKioL1Pnh1_BrCEqAADmho95wNE429.jpg "Title =" 333.jpg" alt = "wkiol1pnh1_brceqaadmho95wne429.jpg"/>


Gevent itself has a gevent. queue. I did not test it myself, but I talked about it again from foreigners. In big data, it is better than the queue of pyhton queue itself.


The queue function of gevent queue is rich!

>>> queue = gevent.queue.Queue()>>> queue.put(1)>>> queue.put(2)>>> queue.put(StopIteration)>>> for item in queue:...    print item12

Original article: http://rfyiamcool.blog.51cto.com/1030776/1538367


The following is a complete example:


import gevent from gevent.queue import Queuetasks = Queue()def worker(n):    while not tasks.empty():        task = tasks.get()        print(‘Worker %s got task %s‘ % (n, task))        gevent.sleep(0)    print(‘Quitting time!‘)def boss():    for i in xrange(1,25):        tasks.put_nowait(i)gevent.spawn(boss).join()gevent.joinall([    gevent.spawn(worker, ‘fuck shencan‘),    gevent.spawn(worker, ‘fuck zb‘),    gevent.spawn(worker, ‘fuck liudehua‘),])


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/56/wKiom1PnhtzwO_JeAAKZRTYXFXo430.jpg "Title =" 444.jpg" alt = "wkiom1pnhtzwo_jeaakzrtyxfxow.jpg"/>


The gevent queue method is still very good. For example, put push queue and get queue. You can add several options for blocking and timeout.


Put (item, block = true, timeout = none) put data into the queue, optional whether to block and timeout put_nowait (item) non-blocking put data into the queue, when the queue is full, full exceptionget (Block = true, timeout = none) is thrown to read data from the queue. Optional, whether to block or not, and the timeout value get_nowait () is used to read data from the queue, if the queue is empty, empty exceptionpeek (Block = true, timeout = none) and get () are thrown. However, the obtained data will not be removed from the queue. peek_nowait () is similar to get_nowait () Empty () if the queue is empty, return truefull (). If the queue is full, return trueqsize (). Return the queue length, that is, the number of data in the queue, rather than the maxlength during Queue (maxlength) initialization.


Original article: http://rfyiamcool.blog.51cto.com/1030776/1538367


The execution status of a gevent is determined.

Started indicates whether the gevent has started ready () indicates whether the gevent has stopped successful () indicates whether the gevent has stopped and no exception value is thrown -- any, the value exception returned by the gevent -- gevent exception, an error occurred while the gevent instance was not under arrest.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/57/wKioL1PnjTOgO7K3AAOYOijH5_w822.jpg "Title =" aaa.jpg "alt =" wkiol1pnjtogo7k3aaoyoijh5_w822.jpg "/>


This article is from "Fengyun, it's her ." Blog, declined to reprint!

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.