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

      

  • 相关阅读:
    svn 相关
    Xcode 升级到7.2版本出现崩溃想象
    IOS UILabel用UITapGestureRecognizer实现点击事件
    JSon解析
    IOS StoryBoard下的视图跳转
    Http中的get和post的区别
    swift-delegate(代理)或者block传值
    swift-自定义TabBar工具栏
    利用POPAnimatableProperty属性来实现动画倒计时
    NSLayoutConstraints加动画来改变约束
  • 原文地址:https://www.cnblogs.com/7134g/p/11730025.html
Copyright © 2020-2023  润新知