164 python Network programming-protocol (gevent version)

Keywords: Python network

greenlet has implemented the protocol, but this manual switch is too troublesome, don't worry, python also has a more powerful module gevent than greenlet and can automatically switch tasks.

The principle is that when a greenlet encounters IO (i.e. input output input and output, such as network, file operation, etc.) operations, such as access to the network, it will automatically switch to other greenlets, until the IO operation is completed, and then switch back to continue execution at an appropriate time.

Because IO operation is very time-consuming, it often makes the program in a waiting state. With gevent switching the protocol automatically for us, we can ensure that there is always greenlet running, not waiting for IO.

1. Use of gevent

#coding=utf-8

#Use python 2 to execute this program

import gevent

def f(n):
    for i in range(n):
        print gevent.getcurrent(), i

g1 = gevent.spawn(f, 5)
g2 = gevent.spawn(f, 5)
g3 = gevent.spawn(f, 5)
g1.join()
g2.join()
g3.join()

Operation results

<Greenlet at 0x10e49f550: f(5)> 0
<Greenlet at 0x10e49f550: f(5)> 1
<Greenlet at 0x10e49f550: f(5)> 2
<Greenlet at 0x10e49f550: f(5)> 3
<Greenlet at 0x10e49f550: f(5)> 4
<Greenlet at 0x10e49f910: f(5)> 0
<Greenlet at 0x10e49f910: f(5)> 1
<Greenlet at 0x10e49f910: f(5)> 2
<Greenlet at 0x10e49f910: f(5)> 3
<Greenlet at 0x10e49f910: f(5)> 4
<Greenlet at 0x10e49f4b0: f(5)> 0
<Greenlet at 0x10e49f4b0: f(5)> 1
<Greenlet at 0x10e49f4b0: f(5)> 2
<Greenlet at 0x10e49f4b0: f(5)> 3
<Greenlet at 0x10e49f4b0: f(5)> 4

As you can see, the three greenlet s run sequentially rather than alternately

2. gevent handover execution

import gevent

def f(n):
    for i in range(n):
        print gevent.getcurrent(), i
        #To simulate a time-consuming operation, note that it is not sleep in the time module
        gevent.sleep(1)

g1 = gevent.spawn(f, 5)
g2 = gevent.spawn(f, 5)
g3 = gevent.spawn(f, 5)
g1.join()
g2.join()
g3.join()

Operation results

<Greenlet at 0x7fa70ffa1c30: f(5)> 0
<Greenlet at 0x7fa70ffa1870: f(5)> 0
<Greenlet at 0x7fa70ffa1eb0: f(5)> 0
<Greenlet at 0x7fa70ffa1c30: f(5)> 1
<Greenlet at 0x7fa70ffa1870: f(5)> 1
<Greenlet at 0x7fa70ffa1eb0: f(5)> 1
<Greenlet at 0x7fa70ffa1c30: f(5)> 2
<Greenlet at 0x7fa70ffa1870: f(5)> 2
<Greenlet at 0x7fa70ffa1eb0: f(5)> 2
<Greenlet at 0x7fa70ffa1c30: f(5)> 3
<Greenlet at 0x7fa70ffa1870: f(5)> 3
<Greenlet at 0x7fa70ffa1eb0: f(5)> 3
<Greenlet at 0x7fa70ffa1c30: f(5)> 4
<Greenlet at 0x7fa70ffa1870: f(5)> 4
<Greenlet at 0x7fa70ffa1eb0: f(5)> 4

Three greenlet s run alternately

3. gevent concurrent Downloader

Of course, in the actual code, we don't use gevent.sleep() to switch the protocol, but when we perform the IO operation, gevent automatically switches. The code is as follows:

#coding=utf-8

from gevent import monkey; 
import gevent
import urllib2

#I need this sentence when I have IO to do it.
monkey.patch_all()

def myDownLoad(url):
    print('GET: %s' % url)
    resp = urllib2.urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

gevent.joinall([
        gevent.spawn(myDownLoad, 'http://www.baidu.com/'),
        gevent.spawn(myDownLoad, 'http://www.itcast.cn/'),
        gevent.spawn(myDownLoad, 'http://www.itheima.com/'),
])

Operation results

GET: http://www.baidu.com/
GET: http://www.itcast.cn/
GET: http://www.itheima.com/
102247 bytes received from http://www.baidu.com/.
166903 bytes received from http://www.itheima.com/.
162294 bytes received from http://www.itcast.cn/.

From above, we can see that the information about baidu is sent first, then itcast and itheima, but the order of receiving data is not necessarily the same as the order of sending data, which also reflects asynchrony, that is, when the data will be received is uncertain, and the order is not necessarily the same.

Posted by wattsup88 on Mon, 19 Aug 2019 03:26:10 -0700