Python——gevent&asyncio模块(线程模块)
gevent模块
1. 第三方模块,需要自行安装。
2. 使用比较方便。
from gevent import monkey
import gevent
import time
monkey.patch_all()
def set():
print('1.1')
gevent.sleep(1) #不写monkey时,只有这样写才会进行协程的切换工作。
print('1.2')
def sleep():
print('2.1')
time.sleep(1) #这样写,不会在IO时进行切换。只有写monkey时才会进行。
print('2.2')
g1 = gevent.spawn(set)
g2 = gevent.spawn(sleep)
g1.join()
g2.join()
#gevent.joinall([g1,g2]) 如果有多个,可以这么写。
print(g1.value) #需要等待协程完成后才能获取,如果之前获取了,就是None
用协程简单实现一个socket
1. 当没有人连接时,会卡在accept中,
2. 有人连接后,会创建一个协程进行后面的处理。
3. 首先得等待主程序是否有阻塞,如果有那么进入协程中。
4. 协程中的recv如果也阻塞,那么再切回去。
5. 没有阻塞继续进行。
因为一直都会有accpet在,就会一直有io,所以不用join也可以。
############server##############
from gevent import monkey
import socket
import gevent
monkey.patch_all()
sk = socket.socket()
sk.bind(('127.0.0.1',9000))
sk.listen(20)
def up(conn):
while True:
msg = conn.recv(1024).decode('utf-8')
print(msg.upper())
conn.send(msg.upper().encode('utf-8'))
while True:
conn,_ = sk.accept()
gevent.spawn(up,conn)
###########clinet#############
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',9000))
sk.send(b'hahaha')
print(sk.recv(1024))
asyncio模块
1. 底层使用基于yeild的内置模块。
单任务+没有返回值。
import asyncio
async def func():
print('1.1')
await asyncio.sleep(0.5) #固定格式,如果没有async在def前,那么就会报错。
print('1.2')
async def slp():
print('2.1')
await asyncio.sleep(1)
print('2.2')
loop = asyncio.get_event_loop() #获取时间循环对象
loop.run_until_complete(func()) #启动单个任务
多任务+没有返回值:
import asyncio
async def func():
print('1.1')
await asyncio.sleep(0.5) #固定格式,如果没有async在def前,那么就会报错。
print('1.2')
async def slp():
print('2.1')
await asyncio.sleep(1)
print('2.2')
loop = asyncio.get_event_loop() #获取时间循环对象
wait_obj= asyncio.wait([func(),slp()]) #加入一个列表中
loop.run_until_complete(wait_obj) #循环列表,运行。
多任务+有返回值+任务按照顺序回来
import asyncio
async def func():
print('1.1')
await asyncio.sleep(0.5)
print('1.2')
return 111
async def slp():
print('2.1')
await asyncio.sleep(1)
print('2.2')
return 222
loop = asyncio.get_event_loop()
t1 = loop.create_task(func())
t2 = loop.create_task(slp())
task_li = [t1,t2]
wait_obj = asyncio.wait([t1,t2])
loop.run_until_complete(wait_obj)
for t in task_li:print(t.result())
多任务+先到先去结果
import asyncio
async def func(i):
print(f'{i}=========1.1')
await asyncio.sleep(0.5)
print(f'{i}=========1.2')
return 111
async def slp(i):
print('2.1')
await asyncio.sleep(1)
print('2.2')
return 222
async def main():
task_li = []
for i in range(10):
task = asyncio.ensure_future(func(i))
task_li.append(task)
for ret in asyncio.as_completed(task_li):
res = await ret
print(res)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

浙公网安备 33010602011771号