Python 的 asyncio 里面可以通过asyncio.get_running_loop().run_in_executor()来让一些不占用cpu的普通异步事件变成不影响协程调度的异步事件,比如说一些多线程的sleep等待或其他进程的计算之类的,亦或者是老牌同步请求库requests库也可以直接转成协程,Rust怎么实现这种功能呢?
比如说类似这样的Python脚本怎么翻译成Rust(Python有GIL,才另起一个进程模拟服务器,Rust用线程也可以)
import multiprocessing
import threading
import asyncio
import time
n = 12
# 新建进程启动模拟服务器和启动协程客户端
def main():
host, listener = multiprocessing.Pipe()
multiprocessing.Process(target=server, args=(listener, )).start()
time.sleep(1)
asyncio.run(client(host))
# 客户端同时发出n个协程异步请求
async def client(host):
t = time.time()
print('start')
loop = asyncio.get_running_loop()
responses = await asyncio.gather(*(
request(index, host, loop)
for index in range(n)
))
print(f'end: {time.time() - t: .3f}s')
print(responses)
# 通过run_in_executor把多进程的异步事件转成协程异步事件
async def request(index, host, loop):
t = time.time()
print('client start', index)
response = await loop.run_in_executor(None, connect, index, host)
print('client end', index, f'{time.time() - t:.3f}s')
return response
# 用多进程管道来模拟请求
def connect(index, host):
sender, receiver = multiprocessing.Pipe()
host.send((index, sender))
return receiver.recv()
# 模拟服务器异步监听n个请求
def server(listener):
print('open server')
method = threading.Thread
# method = multiprocessing.Process
multi = []
for _ in range(n):
multi.append(method(target=api, args=listener.recv()))
multi[-1].start()
for m in multi:
m.join()
print('close server')
# 模拟服务器处理请求
def api(index, sender):
print('server start', index)
time.sleep(1)
# for _ in range(10 ** 7):
# pass
sender.send(f'response for {index}')
print('server end', index)
if __name__ == '__main__':
main()
1
共 1 条评论, 1 页
评论区
写评论贴下使用 tokio的例子 供参考