I am trying to run a snippet of my code in Jupyter notebook, but it's failing. However the same snippet runs as script as well as a python script.
code :
import asyncio
async def a_square(x):
print(f'Asynchronously squaring {x}!')
return x ** 2
asyncio.run(a_square(2))
Error in Jupyter
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-4-e2feaf9936d2> in <module>
5 return x ** 2
6
----> 7 asyncio.run(a_square(2))
/usr/lib/python3.8/asyncio/runners.py in run(main, debug)
31 """
32 if events._get_running_loop() is not None:
---> 33 raise RuntimeError(
34 "asyncio.run() cannot be called from a running event loop")
35
RuntimeError: asyncio.run() cannot be called from a running event loop
Python path, version are same for all. The following statements gave same output for Jupyter, Ipython as well as when executed from script.
import sys
print(sys.executable)
print(sys.version)
output :
/usr/bin/python3
3.8.5 (default, Jul 27 2020, 08:42:51)
[GCC 10.1.0]