0

I tried to run the following program in Python 3.6.4

import asyncio
import concurrent.futures
import requests

async def main():

    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:

        loop = asyncio.get_event_loop()
        futures = [
            loop.run_in_executor(
                executor, 
                requests.get, 
                'http://example.org/'
            )
            for i in range(20)
        ]
        for response in await asyncio.gather(*futures):
            pass


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

However, I got an error, saying that RuntimeError: This event loop is already running.

Here is the complete error message:

Traceback (most recent call last):

  File "<ipython-input-1-28cf105e6739>", line 1, in <module>
    runfile('C:/asyncio_test.py', wdir='C:/')

  File "c:\program files\python36\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "c:\program files\python36\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/asyncio_test.py", line 30, in <module>
    loop.run_until_complete(main())

  File "c:\program files\python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()

  File "c:\program files\python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')

RuntimeError: This event loop is already running

I then tried to run the program in Ubuntu 16.10

File "asyncio_test.py", line 18
    for response in await asyncio.gather(*futures):
                                ^
SyntaxError: invalid syntax

How to solve the problem?

Thank you very much.

3
  • Please add the full error traceback to your question. Commented Apr 12, 2018 at 4:10
  • Full error traceback has been added. Commented Apr 12, 2018 at 4:15
  • Your code does not seem to be run directly. How do you start it? Commented Apr 12, 2018 at 4:19

2 Answers 2

1

I can run your code on Ubuntu 16.04 without any problems.I think this is Windows signal handlers problem.Windows does not have signals and resolves if your code should exit in a different manner for console and GUi programs .Read more console control handdlers

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

5 Comments

Thank you, Richard. How to modify the program to run in Windows?
I do not know,I have not worked with Windows 3,4 years.
I also tried to run the program in Ubuntu but there was an error. Please refer to the last part of the question.
What version of Python are you using on Ubuntu?
python --version Python 3.6.3 :: Anaconda, Inc.
1

Try to change the URL to, say, http://www.google.com and update Python to the latest version.

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.