import asyncio import time async def job(t): # async 形式的功能 print('Start job ', t) await asyncio.sleep(t) # 等待 "t" 秒, 期间切换其他任务 print('Job ', t, ' takes ', t, ' s') async def main(loop): # async 形式的功能 tasks = [ loop.create_task(job(t)) for t in range(1, 3) ] # 创建任务, 但是不执行 await asyncio.wait(tasks) # 执行并等待所有任务完成 t1 = time.time() loop = asyncio.get_event_loop() # 建立 loop loop.run_until_complete(main(loop)) # 执行 loop,并且等待所有任务结束 loop.close() # 关闭 loop print("Async total time : ", time.time() - t1)
执行结果:
Start job 1
Start job 2
Job 1 takes 1 s
Job 2 takes 2 s
Async total time : 2.01411509513855
from greenlet import greenlet def foo(): print("foo") bar() def bar(): a = 3 + 1 print(a) gr2.switch() print("end bar") gr1 = greenlet(bar) gr2 = greenlet(foo) gr1.switch()
执行结果:
4
foo
4
end bar
import requests
import gevent
def f(url):
print('GET: %s' % url)
data = requests.get(url).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/'),
])
print("End of File")
执行结果: