• 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))
  • 相关阅读:
    机器语言 汇编语言 C C++ Java C# javaScript Go 编译型语言 解释型语言
    计算机历史(二战前)
    可维护性组件的编写原则
    the lasted discuss about h5 optimize
    The last discussion about the inherit
    The last discussion about the prototype
    font-size line-height vertual-align的复杂关系
    vertical-align
    retina屏 适配问题
    XMLHttpRequest2.0的进步之处
  • 原文地址:https://www.cnblogs.com/liown/p/8809582.html
Copyright © 2020-2023  润新知