Python异步IO错误
我尝试在Python3.6.4中运行以下程序Python异步IO错误,python,python-asyncio,Python,Python Asyncio,我尝试在Python3.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 = [ l
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())
但是,我得到了一个错误,说RuntimeError:这个事件循环已经在运行了
以下是完整的错误消息:
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
如何解决这个问题
非常感谢。我可以在Ubuntu 16.04上运行你的代码,没有任何问题。我认为这是Windows信号处理程序的问题。Windows没有信号,并且可以解决你的代码是否应该以不同的方式退出控制台和GUi程序。阅读更多内容。我可以在Ubuntu 16.04上运行你的代码,没有任何问题。我认为这是错误的Windows信号处理程序问题。Windows没有信号,可以解决您的代码是否应以不同的方式退出控制台和GUi程序。请阅读更多内容,例如尝试将URL更改为http://www.google.com
并将Python更新至最新版本。尝试将URL更改为,http://www.google.com
并将Python更新至最新版本。请将完整错误回溯添加到您的问题中。已添加完整错误回溯。您的代码似乎无法直接运行。如何启动?请将完整错误回溯添加到问题中。已添加完整错误回溯。您的代码似乎无法直接运行。你是怎么开始的?谢谢你Richard。如何修改程序以在Windows中运行?我不知道,我已经3,4年没有使用Windows了。我也尝试在Ubuntu中运行该程序,但出现了一个错误。请参考问题的最后一部分。你在Ubuntu上使用的是什么版本的Python?Python——版本Python 3.6.3::Anaconda,Inc.谢谢你,Richard。如何修改程序以在Windows中运行?我不知道,我已经3,4年没有使用Windows了。我也尝试在Ubuntu中运行该程序,但出现了一个错误。请参考问题的最后一部分。你在Ubuntu上使用的是什么版本的Python?Python——版本Python 3.6.3::Anaconda,Inc。
File "asyncio_test.py", line 18
for response in await asyncio.gather(*futures):
^
SyntaxError: invalid syntax