国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python greenlet和gevent使用代碼示例解析

瀏覽:13日期:2022-07-31 13:55:56

greenlet示例

greenlet微線程,允許在線程中手動切換

示例1,線程切換

from greenlet import greenletdef test1(x,y): z = gr2.switch(x+y) print(z)def test2(u): print(u) gr1.switch(42)gr1 = greenlet(test1)gr2 = greenlet(test2)gr1.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)

打印結果:

helloworld42

示例2

from greenlet import greenletdef eat(name): print(’%s eat 1’ %name) g2.switch(’egon’) print(’%s eat 2’ %name) g2.switch()def play(name): print(’%s play 1’ %name) g1.switch() print(’%s play 2’ %name)g1=greenlet(eat)g2=greenlet(play)g1.switch(’egon’)#可以在第一次switch時傳入參數,以后都不需要

g1.switch(’egon’)#可以在第一次switch時傳入參數,以后都不需要

gevent

gevent基于greenlet,遇到IO操作自動切換,IO操作比如網絡請求,或使用 gevent.sleep(0)強制切換.

示例1

import geventdef func1(): print('start func1') gevent.sleep(1) print('end func1')def func2(): print('start func2') gevent.sleep(1) print('end func2')gevent.joinall( [ gevent.spawn(func1), gevent.spawn(func2) ])

執行結果:

start func1start func2end func1end func2``

示例2: gevent使用monkey對所有系統自帶的IO操作打patch

```pythonfrom gevent import monkey;monkey.patch_all()import geventimport timedef eat(): print(’eat food 1’) time.sleep(2) # 會自動的跳轉到play print(’eat food 2’)def play(): print(’play 1’) time.sleep(1) # 會自動的跳轉到eat print(’play 2’)g1=gevent.spawn(eat)g2=gevent.spawn(play)gevent.joinall([g1,g2])print(’end’)

執行結果

eat food 1play 1play 2eat food 2end

示例3,發送請求

from gevent import monkey; monkey.patch_all()import geventimport requestsdef f(url): print(’GET: %s’ % url) resp = requests.get(url) data = resp.text print(’%d bytes received from %s.’ % (len(data), url))gevent.joinall([ gevent.spawn(f, ’https://www.python.org/’), gevent.spawn(f, ’https://www.yahoo.com/’), gevent.spawn(f, ’https://github.com/’), gevent.spawn(f, ’https://github.com/’), gevent.spawn(f, ’https://github.com/’), gevent.spawn(f, ’https://github.com/’), gevent.spawn(f, ’https://github.com/’),])

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

import geventfrom gevent import socketurls = [’www.baidu.com’, ’www.163.com’, ’www.qq.com’]jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]gevent.joinall(jobs, timeout=2)print([job.value for job in jobs])或使用patch_socket()from gevent import monkey; monkey.patch_socket()import geventdef f(n): for i in range(n): print(gevent.getcurrent(), i) gevent.sleep(0) # 不加的話不會交替執行g1 = gevent.spawn(f, 5)g2 = gevent.spawn(f, 5)g3 = gevent.spawn(f, 5)g1.join()g2.join()g3.join()

示例5:隊列中使用gevent.sleet(0)強制切換到其他線程

import geventfrom gevent.queue import Queuedef func(): for i in range(10): print('int the func') q.put(f'test{i}') gevent.sleep(0)def func2(): for i in range(10): print('int the func2') res = q.get() print('--->',res)q = Queue()gevent.joinall( [ gevent.spawn(func2), gevent.spawn(func), ])

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 最新国产中文字幕 | 美女被免费网站在线视频软件 | 日韩一级片在线观看 | 99国产在线视频 | 欧美性生交大片免费看 | 99久久亚洲国产高清观看 | 亚洲国产日韩欧美综合久久 | 久久精品a一国产成人免费网站 | 日韩亚洲一区二区三区 | 亚洲精品国产福利一区二区三区 | 日本一区视频在线观看 | 99在线免费观看视频 | 亚洲欧美国产日韩天堂在线视 | 中国老太卖淫播放毛片 | 亚洲成人在线免费观看 | 国产男女乱淫真视频全程播放 | 特级毛片全部免费播放a一级 | 又摸又揉又黄又爽的视频 | 性夜黄a爽爽免费视频国产 性夜影院爽黄a爽免费看网站 | 久久污| 中文字字幕码一二区 | 国产在线91精品天天更新 | 美女的被男人桶爽网站 | 亚洲视频免费在线观看 | 成人伊人青草久久综合网 | 国产视频www | 国产亚洲一区二区三区在线观看 | 久久亚洲欧洲日产国码 | 亚洲炮网 | 欧美最猛性xxxxx亚洲精品 | 六月成人网 | 成年人国产 | 亚洲成人精品 | 国产亚洲精品看片在线观看 | 91精品国产免费久久久久久 | 国产成人精品日本亚洲语音1 | 欧亚毛片 | 亚洲福利视频一区二区三区 | 国产高清在线精品一区二区三区 | 色www永久免费网站国产 | 亚洲国产精品自在现线让你爽 |