Python greenlet和gevent使用代碼示例解析
greenlet示例
greenlet微線程,允許在線程中手動(dòng)切換
示例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是兩個(gè)greenlet線程,使用gr1.switch(..)啟動(dòng)gr1,gr1執(zhí)行test1,切換到gr2,gr2執(zhí)行test2打印helloworld,然后切換回gr1,z獲取
到返回值42,并打印.
執(zhí)行順序?yàn)?
gr1.switch('hello',’world’) -> test1(’hello’,’world’)->
gr2.switch(’helloword’)->test2(’helloworld’)->print(’helloworld’)
->gr1.switch(42)->z=42->print(42)
打印結(jié)果:
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時(shí)傳入?yún)?shù),以后都不需要
g1.switch(’egon’)#可以在第一次switch時(shí)傳入?yún)?shù),以后都不需要
gevent
gevent基于greenlet,遇到IO操作自動(dòng)切換,IO操作比如網(wǎng)絡(luò)請(qǐng)求,或使用 gevent.sleep(0)強(qiáng)制切換.
示例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) ])
執(zhí)行結(jié)果:
start func1start func2end func1end func2``
示例2: gevent使用monkey對(duì)所有系統(tǒng)自帶的IO操作打patch
```pythonfrom gevent import monkey;monkey.patch_all()import geventimport timedef eat(): print(’eat food 1’) time.sleep(2) # 會(huì)自動(dòng)的跳轉(zhuǎn)到play print(’eat food 2’)def play(): print(’play 1’) time.sleep(1) # 會(huì)自動(dòng)的跳轉(zhuǎn)到eat print(’play 2’)g1=gevent.spawn(eat)g2=gevent.spawn(play)gevent.joinall([g1,g2])print(’end’)
執(zhí)行結(jié)果
eat food 1play 1play 2eat food 2end
示例3,發(fā)送請(qǐng)求
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替代系統(tǒng)的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) # 不加的話不會(huì)交替執(zhí)行g(shù)1 = gevent.spawn(f, 5)g2 = gevent.spawn(f, 5)g3 = gevent.spawn(f, 5)g1.join()g2.join()g3.join()
示例5:隊(duì)列中使用gevent.sleet(0)強(qiáng)制切換到其他線程
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), ])
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫(kù)用法分享3. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說(shuō)明4. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總5. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】6. Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新留言效果7. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效8. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能9. Python編寫nmap掃描工具10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
