asyncio與tornado異步任務

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單任務異步:async

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庫

或者:tornado

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併發執行:oop

一樣是利用asyncio,tornado的run_sync方法不支持多任務。url

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))
相關文章
相關標籤/搜索