Is it possible to run only a single step of the asyncio event loop

前端 未结 3 1342
北恋
北恋 2020-12-10 05:31

I\'m working on a simple graphical network application, using asyncio and tkinter. I\'m running into the problem of combining the asyncio event loop with Tk\'s mainloop. If

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 06:01

    The missing of public method like loop.run_once() is intentional. Not every supported event loop has a method to iterate one step. Often underlying API has methods for creating event loop and running it forever but emulating single step may be very ineffective.

    If you really need it you may implement single-step iteration easy:

    import asyncio
    
    
    def run_once(loop):
        loop.call_soon(loop.stop)
        loop.run_forever()
    
    
    loop = asyncio.get_event_loop()
    
    for i in range(100):
        print('Iteration', i)
        run_once(loop)
    

提交回复
热议问题