• asyncio动态添加任务


    asyncio.run_forever()下动态添加任务

    方法一、asyncio.run_coroutine_threadsafe(coroutine, loop)

    方法二、asyncio.call_soon_threadsafe(callback, *args, context=None)

    方法一、实例run_coroutine_threadsafe

    通过添加新的coroutin对象,放入loop内,动态添加。(爬虫正解)

    import asyncio
    import time
    
    from threading import Thread
    
    
    def start_loop(loop):
        asyncio.set_event_loop(loop)
        print("start loop", time.time())
        loop.run_forever()
    
    
    async def do_some_work(x):
        print('start {}'.format(x))
        await asyncio.sleep(x)
        print('Done after {}s'.format(x))
    
    
    new_loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=(new_loop,))
    t.start()
    
    asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop)
    asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop)
    

      

    方法二、实例call_soon_threadsafe

    如果是io操作,就会相当于变成线程去阻塞执行添加进去的函数,密集型则没关系。

    import asyncio
    import time
    from threading import Thread
    
    
    def start_loop(loop):
        asyncio.set_event_loop(loop)
        print("start loop")
        loop.run_forever()
    
    
    def more_work(x):
        print('start work {}'.format(x))
        time.sleep(x)
        print('Finished work {}'.format(x))
    
    
    new_loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=(new_loop,))
    t.start()
    
    new_loop.call_soon_threadsafe(more_work, 6)
    new_loop.call_soon_threadsafe(more_work, 3)
    

      

  • 相关阅读:
    自动换行的两种代码(C#)
    C#的SubString(int start,int end);
    php数组添加元素的方法
    php通过生成动态变量(变量名中还有变量)
    php定义空的数组
    php的$GLOBALS例子
    WINCRIS的使用
    java的PreparedStatement中使用like时的问题
    php发送get请求
    在HTML中使用object和embed标签插入视频
  • 原文地址:https://www.cnblogs.com/7134g/p/11730025.html
Copyright © 2020-2023  润新知