这是一个简单易用的异步执行模块,可以使原有方法轻松变为异步执行。
- Python3
通过pip命令安装:
pip install async4s
- async by thread
import time
from async4s.mw.thread import Master, Worker
def work(i):
time.sleep(i)
return i
def callback(results):
print(results)
print(time.perf_counter())
workers = [Worker(work, i) for i in range(5)]
m = Master(workers, callback)
print("main")
m.wait()
print(time.perf_counter())
- async by asyncio
import time
import asyncio
from async4s.mw.coroutine import Master, Worker
async def work(i):
await asyncio.sleep(i)
return i
def callback(results):
print(results)
print(time.perf_counter())
workders = (Worker(work, i) for i in range(5))
master = Master(workders, callback=callback)
print("main")
master.wait()
print(time.perf_counter())
- Birth
- Master-Worker mode
- both implement by thread and asyncio