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单任务异步:

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))

 

转载于:https://www.cnblogs.com/liown/p/8809582.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值