【发布时间】:2015-04-07 22:52:09
【问题描述】:
我正在运行以下代码,它通过 aiohttp 发出 5 个请求:
import aiohttp
import asyncio
def fetch_page(url, idx):
try:
url = 'http://google.com'
response = yield from aiohttp.request('GET', url)
print(response.status)
except Exception as e:
print(e)
def main():
try:
url = 'http://google.com'
urls = [url] * 5
coros = []
for idx, url in enumerate(urls):
coros.append(asyncio.Task(fetch_page(url, idx)))
yield from asyncio.gather(*coros)
except Exception as e:
print(e)
if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except Exception as e:
print(e)
输出:
200
200
200
200
200
Exception ignored in: Exception ignored in: Exception ignored in: Exception ignored in: Exception ignored in:
注意:没有关于异常是什么/在哪里的其他信息。
这是什么原因造成的,有什么调试方法吗?
【问题讨论】:
-
请注意,将整个函数包装在
try: ... except Exception中并不是最佳做法。您在 in 中运行什么代码 - 是来自 IDE 的 "Exception denied" 消息吗? -
我添加了这些以尝试捕获错误 - 实际上打印了“忽略异常”而没有
try... except存在。我在想aiohttp或asyncio中的某些东西正在消除异常。我在 Python 3.4 中通过终端执行的脚本中运行它。 -
看起来这可能与 this python bug 有关。不过,我不确定为什么首先会发生无法引发的异常;我无法在我的系统上重现它。
-
@okoboko 另请参阅this bug 末尾的 cmets,其中在退出
asyncio应用程序时提到了同样的错误。您使用的是 Python 3.4.3 吗? -
嗯,这确实很奇怪。我正在使用 3.5.0a1。很高兴它似乎与我的代码无关。
标签: python python-3.x python-asyncio aiohttp