Python greenlet和gevent使用代碼示例解析
greenlet示例
greenlet微線程,允許在線程中手動切換
示例1,線程切換
from greenlet import greenlet
def 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執(zhí)行test1,切換到gr2,gr2執(zhí)行test2打印helloworld,然后切換回gr1,z獲取
到返回值42,并打印.
執(zhí)行順序為:
gr1.switch("hello",'world') -> test1('hello','world')->
gr2.switch('helloword')->test2('helloworld')->print('helloworld')
->gr1.switch(42)->z=42->print(42)
打印結(jié)果:
helloworld
42
示例2
from greenlet import greenlet
def 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時傳入?yún)?shù),以后都不需要
g1.switch('egon')#可以在第一次switch時傳入?yún)?shù),以后都不需要
gevent
gevent基于greenlet,遇到IO操作自動切換,IO操作比如網(wǎng)絡(luò)請求,或使用 gevent.sleep(0)強(qiáng)制切換.
示例1
import gevent
def 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 func1
start func2
end func1
end func2
``
示例2: gevent使用monkey對所有系統(tǒng)自帶的IO操作打patch
```python
from gevent import monkey;monkey.patch_all()
import gevent
import time
def eat():
print('eat food 1')
time.sleep(2) # 會自動的跳轉(zhuǎn)到play
print('eat food 2')
def play():
print('play 1')
time.sleep(1) # 會自動的跳轉(zhuǎn)到eat
print('play 2')
g1=gevent.spawn(eat)
g2=gevent.spawn(play)
gevent.joinall([g1,g2])
print('end')
執(zhí)行結(jié)果
eat food 1
play 1
play 2
eat food 2
end
示例3,發(fā)送請求
from gevent import monkey; monkey.patch_all()
import gevent
import requests
def 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 gevent
from gevent import socket
urls = ['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 gevent
def f(n):
for i in range(n):
print(gevent.getcurrent(), i)
gevent.sleep(0) # 不加的話不會交替執(zhí)行
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)強(qiáng)制切換到其他線程
import gevent
from gevent.queue import Queue
def 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),
]
)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python協(xié)程操作之gevent(yield阻塞,greenlet),協(xié)程實現(xiàn)多任務(wù)(有規(guī)律的交替協(xié)作執(zhí)行)用法詳解
- Python協(xié)程 yield與協(xié)程greenlet簡單用法示例
- 使用Python中的greenlet包實現(xiàn)并發(fā)編程的入門教程
- Python greenlet實現(xiàn)原理和使用示例
- Python中g(shù)event模塊協(xié)程使用
- 簡單了解python gevent 協(xié)程使用及作用
- 詳解python之協(xié)程gevent模塊
- Python中的協(xié)程(Coroutine)操作模塊(greenlet、gevent)
相關(guān)文章
Python創(chuàng)建簡單的神經(jīng)網(wǎng)絡(luò)實例講解
在本篇文章里小編給大家整理的是一篇關(guān)于如何在Python中創(chuàng)建一個簡單的神經(jīng)網(wǎng)絡(luò)的相關(guān)知識點,有興趣的朋友們可以參考下。2021-01-01
解決os.path.isdir() 判斷文件夾卻返回false的問題
今天小編就為大家分享一篇解決os.path.isdir() 判斷文件夾卻返回false的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python中elasticsearch插入和更新數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了Python中elasticsearch插入和更新數(shù)據(jù)的實現(xiàn)方法,需要的朋友可以參考下2018-04-04
Python + Requests + Unittest接口自動化測試實例分析
這篇文章主要介紹了Python + Requests + Unittest接口自動化測試,結(jié)合具體實例形式分析了Python使用Requests與Unittest模塊實現(xiàn)接口自動化測試相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Python標(biāo)準(zhǔn)庫筆記struct模塊的使用
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫筆記struct模塊的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析
今天小編就為大家分享一篇基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

