Reputation: 487
I'm using the asynchronous functionality of Python. After learn how to use it.
I updated my code to work with data analysis. It works fine in python3.5. However, when I updated from Python3.5 to Python3.6, there is Runtime Error.
My usage is very simple.
First, create an asynchronous usage in a file, named 'runner.py',like this:
import asyncio as aio
def async_usage():
loop = aio.get_event_loop()
task = loop.create_task(some_task())
loop.run_until_comeplete(task)
Then, import it from another file, named "main.py"
import async_usage from runner
async_usage()
When I run main.py then Runtime Error occurrs.
The error says that this event loop is already running.
By the way, these code and running are finished in jupyter notebook
How can I avoid things like this?
I've tried to add code like loop.stop();loop.close()
before I run it, however, it can't work.
I know that only one loop can exit simultaneously, however, I didn't run the loop in runner.py.
How can I solve this?
thx.
Upvotes: 1
Views: 8253
Reputation: 305
It is the problem of the tornado of new versions, please install old version like this.
pip install tornado==4.5.3
Upvotes: 0
Reputation: 487
There is nothing to do with my code, but to do with the notebook. If you are using the notebook, run the code below, you will find out that the current loop is running.
import asyncio as aio
default_loop = aio.get_event_loop()
if default_loop.is_running():
print("The current loop is running!")
For further proving, if you then run the code below, your notebook will shutdown
default_loop.stop()
Then the notebook shell would automatically restart, and what was in your current notebook in the memory is gone.
If you do this in the python shell, nothing would happen.
Therefor, what needed to do is not to use run_until_complete
and the task will automatically run through the current loop.
Upvotes: 1
Reputation: 26
I think you are running on windows platform.
loop = asyncio.get_event_loop() will create a _WindowsSelectorEventLoop object.
the running default is True. so you should remove "loop.run_until_complete(some_task())"
<_WindowsSelectorEventLoop running=True closed=False debug=False>
If you run on linux platform, you will get a _UnixSelectorEventLoop object.
<_UnixSelectorEventLoop running=False closed=False debug=False>
That will be ok.
Upvotes: 0