asyncio单任务异步:
1 import asyncio 2 3 async def get(): 4 url = await WxPay().get_code_url("test",'09234512341',0.01, 5 time_expire=12321321, 6 notify_url='') 7 print(url) 8 9 asyncio.get_event_loop().run_until_complete(get())
asyncio并发执行任务:
import asyncio async def get(): url = yield WxPay().get_code_url("test",'09234512341',0.01, time_expire=412222223, notify_url='') print(url) tasks = [get(), get()] asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))
tornado单任务异步:
from tornado.platform.asyncio import to_asyncio_future,AsyncIOMainLoop import asyncio import datetime async def get(): url = yield WxPay().get_code_url("test",'09234512341',0.01, time_expire=1232131, notify_url='') print(url) AsyncIOMainLoop().install() asyncio.get_event_loop().run_until_complete(to_asyncio_future(get())) #这儿利用的是asyncio库
或者:
from tornado.ioloop import IOLoop async def get(): url = yield WxPay().get_code_url("test",'09234512341',0.01, time_expire=2321321, notify_url='') print(url) IOLoop.instance().run_sync(get)
tornado并发执行:
同样是利用asyncio,tornado的run_sync方法不支持多任务。
from tornado.platform.asyncio import to_asyncio_future,AsyncIOMainLoop import asyncio async def get(): url = yield WxPay().get_code_url("test",'09234512341',0.01, time_expire=23214124, notify_url='') print(url) tasks = [get(), get()] AsyncIOMainLoop().install() asyncio.get_event_loop().run_until_complete(to_asyncio_future(tasks))