GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PythonAdvanced/docs/15.协程-gevent.md at master · feixiao/PythonAdvanced

Expand file tree

/

15.协程-gevent.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
203 lines (162 loc) · 3.75 KB
/

15.协程-gevent.md

Copy path

File metadata and controls

203 lines (162 loc) · 3.75 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

gevent

greenlet 已经实现了协程,但是这个还的人工切换,是不是觉得太麻烦了,不要捉急,python 还有一个比 greenlet 更强大的并且能够自动切换任务的模块gevent

其原理是当一个 greenlet 遇到 IO(指的是 input output 输入输出,比如网络、文件操作等)操作时,比如访问网络,就自动切换到其他的 greenlet,等到 IO 操作完成,再在适当的时候切换回来继续执行。

由于 IO 操作非常耗时,经常使程序处于等待状态,有了 gevent 为我们自动切换协程,就保证总有 greenlet 在运行,而不是等待 IO

一.安装和使用 gevent

1.安装

sudo pip3 install gevent

2.使用

结构:

import gevent

def 函数名(参数列表):
    gevent.getcurrent()  # 自动切换
变量名1 = gevent.spawn(函数名, 参数)
变量名1.join()
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()

运行结果:

<Greenlet at 0x100578e18: f(5)> 0
<Greenlet at 0x100578e18: f(5)> 1
<Greenlet at 0x100578e18: f(5)> 2
<Greenlet at 0x100578e18: f(5)> 3
<Greenlet at 0x100578e18: f(5)> 4
<Greenlet at 0x1007d2048: f(5)> 0
<Greenlet at 0x1007d2048: f(5)> 1
<Greenlet at 0x1007d2048: f(5)> 2
<Greenlet at 0x1007d2048: f(5)> 3
<Greenlet at 0x1007d2048: f(5)> 4
<Greenlet at 0x1007d2158: f(5)> 0
<Greenlet at 0x1007d2158: f(5)> 1
<Greenlet at 0x1007d2158: f(5)> 2
<Greenlet at 0x1007d2158: f(5)> 3
<Greenlet at 0x1007d2158: f(5)> 4

可以看到,3 个 greenlet 是依次运行而不是交替运行

demo02:

import gevent


def f(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        # 用来模拟一个耗时操作,注意不是time模块中的sleep
        gevent.sleep(1)


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

运行结果:

<Greenlet at 0x10492ee18: f(5)> 0
<Greenlet at 0x104b84048: f(5)> 0
<Greenlet at 0x104b84158: f(5)> 0
<Greenlet at 0x10492ee18: f(5)> 1
<Greenlet at 0x104b84048: f(5)> 1
<Greenlet at 0x104b84158: f(5)> 1
<Greenlet at 0x10492ee18: f(5)> 2
<Greenlet at 0x104b84048: f(5)> 2
<Greenlet at 0x104b84158: f(5)> 2
<Greenlet at 0x10492ee18: f(5)> 3
<Greenlet at 0x104b84048: f(5)> 3
<Greenlet at 0x104b84158: f(5)> 3
<Greenlet at 0x10492ee18: f(5)> 4
<Greenlet at 0x104b84048: f(5)> 4
<Greenlet at 0x104b84158: f(5)> 4

可以看出现在程序是交替运行的

二. 给程序打补丁

打补丁前 demo:

from gevent import monkey
import gevent
import random
import time

def coroutine_work(coroutine_name):
    for i in range(10):
        print(coroutine_name, i)
        time.sleep(random.random())

gevent.joinall([
        gevent.spawn(coroutine_work, "work1"),
        gevent.spawn(coroutine_work, "work2")
])

运行结果:

work1 0
work1 1
work1 2
work1 3
work1 4
work1 5
work1 6
work1 7
work1 8
work1 9
work2 0
work2 1
work2 2
work2 3
work2 4
work2 5
work2 6
work2 7
work2 8
work2 9

打补丁后 demo:

from gevent import monkey
import gevent
import random
import time

# 有耗时操作时需要
monkey.patch_all()  # 将程序中用到的耗时操作的代码,换为gevent中自己实现的模块

def coroutine_work(coroutine_name):
    for i in range(10):
        print(coroutine_name, i)
        time.sleep(random.random())

gevent.joinall([
        gevent.spawn(coroutine_work, "work1"),
        gevent.spawn(coroutine_work, "work2")
])

运行结果:

work1 0
work2 0
work2 1
work1 1
work1 2
work1 3
work2 2
work2 3
work1 4
work1 5
work2 4
work2 5
work1 6
work1 7
work2 6
work2 7
work1 8
work1 9
work2 8
work2 9
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.