Python中greenlet和gevent使用示例

[TOC]

greenlet示例

greenlet微线程,允许在线程中手动切换

示例1,线程切换

1from greenlet import greenlet 2 3def test1(x,y): 4 z = gr2.switch(x+y) 5 print(z) 6 7def test2(u): 8 print(u) 9 gr1.switch(42) 10 11gr1 = greenlet(test1) 12gr2 = greenlet(test2) 13gr1.switch("hello",'world')

gr1和gr2是两个greenlet线程,使用gr1.switch(..)启动gr1,gr1执行test1,切换到gr2,gr2执行test2打印helloworld,然后切换回gr1,z获取到返回值42,并打印. 执行顺序为: gr1.switch("hello",'world') -> test1('hello','world')-> gr2.switch('helloword')->test2('helloworld')->print('helloworld') ->gr1.switch(42)->z=42->print(42) 打印结果:

1helloworld 242

示例2

1from greenlet import greenlet 2 3def eat(name): 4 print('%s eat 1' %name) 5 g2.switch('egon') 6 print('%s eat 2' %name) 7 g2.switch() 8def play(name): 9 print('%s play 1' %name) 10 g1.switch() 11 print('%s play 2' %name) 12 13g1=greenlet(eat) 14g2=greenlet(play) 15 16g1.switch('egon')#可以在第一次switch时传入参数,以后都不需要

gevent

gevent基于greenlet,遇到IO操作自动切换,IO操作比如网络请求,或使用 gevent.sleep(0)强制切换.

示例1

1import gevent 2 3def func1(): 4 print("start func1") 5 gevent.sleep(1) 6 print("end func1") 7 8def func2(): 9 print("start func2") 10 gevent.sleep(1) 11 print("end func2") 12 13gevent.joinall( 14 [ 15 gevent.spawn(func1), 16 gevent.spawn(func2) 17 ] 18)

执行结果:

1start func1 2start func2 3end func1 4end func2 5`` 6 7### 示例2: gevent使用monkey对所有系统自带的IO操作打patch 8```python 9from gevent import monkey;monkey.patch_all() 10 11import gevent 12import time 13def eat(): 14 print('eat food 1') 15 time.sleep(2) # 会自动的跳转到play 16 print('eat food 2') 17 18def play(): 19 print('play 1') 20 time.sleep(1) # 会自动的跳转到eat 21 print('play 2') 22 23g1=gevent.spawn(eat) 24g2=gevent.spawn(play) 25gevent.joinall([g1,g2]) 26print('end')

执行结果

1eat food 1 2play 1 3play 2 4eat food 2 5end

示例3,发送请求

1from gevent import monkey; monkey.patch_all() 2import gevent 3import requests 4 5def f(url): 6 print('GET: %s' % url) 7 resp = requests.get(url) 8 data = resp.text 9 print('%d bytes received from %s.' % (len(data), url)) 10 11gevent.joinall([ 12 gevent.spawn(f, 'https://www.python.org/'), 13 gevent.spawn(f, 'https://www.yahoo.com/'), 14 gevent.spawn(f, 'https://github.com/'), 15 gevent.spawn(f, 'https://github.com/'), 16 gevent.spawn(f, 'https://github.com/'), 17 gevent.spawn(f, 'https://github.com/'), 18 gevent.spawn(f, 'https://github.com/'), 19])

示例4:使用gevent的socket替代系统的socket

1import gevent 2from gevent import socket 3 4urls = ['www.baidu.com', 'www.163.com', 'www.qq.com'] 5jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls] 6gevent.joinall(jobs, timeout=2) 7 8print([job.value for job in jobs]) 9 10 11或使用patch_socket() 12from gevent import monkey; monkey.patch_socket() 13import gevent 14 15def f(n): 16 for i in range(n): 17 print(gevent.getcurrent(), i) 18 gevent.sleep(0) # 不加的话不会交替执行 19g1 = gevent.spawn(f, 5) 20g2 = gevent.spawn(f, 5) 21g3 = gevent.spawn(f, 5) 22g1.join() 23g2.join() 24g3.join()

示例5:队列中使用gevent.sleet(0)强制切换到其他线程

1import gevent 2from gevent.queue import Queue 3 4 5def func(): 6 for i in range(10): 7 print("int the func") 8 q.put(f"test{i}") 9 gevent.sleep(0) 10 11def func2(): 12 for i in range(10): 13 print("int the func2") 14 res = q.get() 15 print("--->",res) 16 17q = Queue() 18gevent.joinall( 19 [ 20 gevent.spawn(func2), 21 gevent.spawn(func), 22 ] 23)
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python中greenlet和gevent使用示例 - HelloWorld