1

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.

4
  • See docs.python.org/3/library/asyncio-task.html#task. The task returned by create_task is already running and does not need to be handed over to a event loop. Commented Aug 17, 2018 at 12:07
  • Thanks for ur replying. Please note that, task is created by the loop rather that asyncio module. Due to the doc, task created by loop can be used by the loop.See: docs.python.org/3/library/asyncio-eventloop.html#tasks . Commented Aug 17, 2018 at 12:31
  • Both doc snippets say that the execution of the task is automatically scheduled, or in other words, that you don't have to be passed to run_until_complete because they are already running. You somehow also come to that bottom line in your own answer. Commented Aug 18, 2018 at 6:26
  • Yes, actually, my further code need the result of other tasks and this is the reason why I want to use run_until_complete to wait for tasks to complete. Anyway, I solved this demand by subprocess and use a json file to transfer the result. The main process can wait for the subprocess. This is what I want. Commented Aug 19, 2018 at 13:57

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks dude.Ur solution works. And I answer the question with more details.
0

It is the problem of the tornado of new versions, please install old version like this.

pip install tornado==4.5.3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.