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)
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.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.Source: https://habr.com/ru/post/87793/
All Articles