Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace a closed event loop?

I work mostly in IPython's interactive shell. Sometimes I copy-paste code from the Internet to test it out and work with examples.

If I paste the following code from this tutorial:

import asyncio

async def speak_async():  
    print('OMG asynchronicity!')

loop = asyncio.get_event_loop()  
loop.run_until_complete(speak_async())  
loop.close()  

I will close the loop. The documentation says not to use any methods on an event loop after it's been closed. And async.get_event_loop() will still return that closed loop.

What should I do if I accidentally close an event loop? I would rather not restart the interpreter.

like image 696
leewz Avatar asked Jan 26 '16 18:01

leewz


1 Answers

You could just create and set a new event loop for the current context;

asyncio.set_event_loop(asyncio.new_event_loop())
like image 134
Joachim Isaksson Avatar answered Oct 23 '22 07:10

Joachim Isaksson