• 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))
  • 相关阅读:
    0.Mysql优化框架
    [C]extern char **environ
    [C]Armstrong Number
    [C]struct, union的结合
    [C]enum类型
    [C]fibonacci series, recursion, time.h
    [C/JAVA]二维数组
    [C] char**, swich-case, refactoring, stdbool.h
    hardly/scarcely ... goes by without ...
    [C]随机数生成
  • 原文地址:https://www.cnblogs.com/liown/p/8809582.html
Copyright © 2020-2023  润新知