📜 ⬆️ ⬇️

Python projects within Google Summer of Code - gevent

It is already spring, which means ... that summer is coming soon and another Google Summer of Code is an opportunity to get a lot of experience and even some material reward. I want to talk about one interesting project that you can help during the summer holidays - gevent .

gevent is a Python library that allows you to write asynchronous network applications using the synchronous API. As you may have guessed, she uses for this greenlet 's. I will explain the above with the following code example (taken from the official project repository on bitbucket):

 "" "To complete" "

 urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']

 import gevent
 from gevent import monkey
 monkey.patch_all ()

 import urllib2

 def print_head (url):
     print 'Starting% s'% url
     data = urllib2.urlopen (url) .read ()
     print '% s:% s bytes:% r'% (url, len (data), data [: 50])

 jobs = [gevent.spawn (print_head, url) for url in urls]
 gevent.joinall (jobs)

The main charm of this piece of code is that all requests for retrieving pages using the urllib2.urlopen call urllib2.urlopen executed in parallel. A similar trick can be done with any library that uses the socket module. Intrigued? I even managed to use such powerful libraries as SQLAlchemy in applications written using gevent (of course, you must use DB-API 2.0 adapters with python sockets). Resume is a readable code that performs asynchronous I / O and no callbacks (do you remember Twisted?), Which is also fast. Why am I claiming fast? Because “under the hood” of a gevent - Libevent is a fast and time-tested library that provides an event loop, which is located entirely in C code. And in addition to Libevent there is a DNS resolver and an HTTP server that gevent uses full (modules gevent.dns , gevent.http ). You can verify gevent speed, for example, by following the link and looking at the benchmark results among WSGI servers written in Python.

And now specifically about the ideas on GSoC for gevent, but so far there are only two of them, which, incidentally, does not prevent you from offering your own:

As you know, the greenlet module, which is used in gevent, is just a “squeeze” of Stackless Python. And why gevent does not force to work together with Stackless Python. First, for the latter, there is still no high-quality library that implements non-blocking sockets, and if it appears, users of Stackless Python will be very grateful to you. Secondly, Stackless Python “micro flows” (tasklets) work faster than greenlets, which will give a considerable speed increase in applications written with gevent.
')
As for the implementation of event-handling cycles, I’ll just give my thoughts:

If you are interested, subscribe to the official gevent mailing list, there is just the beginning of a discussion about GSoC projects.

Source: https://habr.com/ru/post/87793/


All Articles