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.
create_taskis already running and does not need to be handed over to a event loop.taskis created by the loop rather that asyncio module. Due to the doc,taskcreated by loop can be used by the loop.See: docs.python.org/3/library/asyncio-eventloop.html#tasks .run_until_completebecause they are already running. You somehow also come to that bottom line in your own answer.run_until_completeto wait for tasks to complete. Anyway, I solved this demand bysubprocessand use a json file to transfer the result. The main process can wait for the subprocess. This is what I want.