ASYNCIO:[错误] 任务被破坏,但它处于挂起状态
Posted
技术标签:
【中文标题】ASYNCIO:[错误] 任务被破坏,但它处于挂起状态【英文标题】:ASYNCIO: [ERROR] Task was destroyed but it is pending 【发布时间】:2017-08-05 01:13:24 【问题描述】:async def run_check(shell_command):
p = await asyncio.create_subprocess_shell(shell_command,
stdin=PIPE, stdout=PIPE, stderr=STDOUT)
fut = p.communicate()
try:
pcap_run = await asyncio.wait_for(fut, timeout=5)
except asyncio.TimeoutError:
p.kill()
await p.communicate()
def get_coros(pcap_list):
for pcap_loc in pcap_list:
for pcap_check in get_pcap_executables():
tmp_coro = (run_check('args'
.format(e=sys.executable, args=args)))
if tmp_coro != False:
coros.append(tmp_coro)
return coros
async def main():
pcap_list_gen = print_dir_cointent()
for pcap_list in pcap_list_gen:
p_coros = get_coros(pcap_list)
for f in asyncio.as_completed(p_coros):
res = await f
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
pcap_list_gen 是包含多个列表的 PCAP 列表生成器。 如果我在一个列表中传递所有 pcap,我会得到 [OS: Error: To many open file],因此决定将它们分组到较小尺寸的列表中并一次处理一个。
每个 pcap_list 是多个 PCAP 的列表。 我希望循环的下一次迭代仅在第一次迭代完成后开始。
for pcap_list in pcap_list_gen:
p_coros = get_coros(pcap_list)
for f in asyncio.as_completed(p_coros):
res = await f
据我所知:第一个循环正在正确执行,因为迭代进入下一个循环会引发错误。
追溯:
Exception ignored in: <generator object BaseSubprocessTransport._connect_pipes at 0x7f7b7c165830>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 188, in _connect_pipes
waiter.set_exception(exc)
File "/usr/lib/python3.5/asyncio/futures.py", line 349, in set_exception
self._schedule_callbacks()
File "/usr/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
self._loop.call_soon(callback, self)
File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
handle = self._call_soon(callback, args)
File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
self._check_closed()
File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
[ERROR] Task was destroyed but it is pending!
task: <Task pending coro=<BaseSubprocessTransport._connect_pipes() done, defined at /usr/lib/python3.5/asyncio/base_subprocess.py:156> wait_for=<Future pending cb=[Task._wakeup()]>>
更多日志
RuntimeError: Event loop is closed
Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_UnixSubprocessTransport closed pid=70435 running stdin=<_UnixWritePipeTransport closing fd=12 open>>>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 126, in __del__
File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 101, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 568, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 560, in write_eof
File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
[ERROR] Task exception was never retrieved
future: <Task finished coro=<ClassificationCheck.run_check() done, defined at ./regression.py:159> exception=RuntimeError('cannot reuse already awaited coroutine',)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
RuntimeError: cannot reuse already awaited coroutine
main()中for循环迭代后_p_coros_的O/P as
P_COROS(1st iteration): [<coroutine object ClassificationCheck.run_check at 0x7f746d984ca8>, <coroutine object ClassificationCheck.run_check at 0x7f746d984db0>, <coroutine object ClassificationCheck.run_check at 0x7f746d984f68>, <coroutine object ClassificationCheck.run_check at 0x7f746d984fc0>]
awating in block 'finally'
awating in block 'finally'
awating in block 'finally'
awating in block 'finally'
P_COROS(2nd iteration): [<coroutine object ClassificationCheck.run_check at 0x7f746d984ca8>, <coroutine object ClassificationCheck.run_check at 0x7f746d984db0>, <coroutine object ClassificationCheck.run_check at 0x7f746d984f68>, <coroutine object ClassificationCheck.run_check at 0x7f746d984fc0>, <coroutine object ClassificationCheck.run_check at 0x7f746d943048>, <coroutine object ClassificationCheck.run_check at 0x7f746d9430f8>]
Traceback (most recent call last):
File "./regression.py", line 325, in <module>
loop.run_until_complete(ClassCheck.main())
File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
return future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "./regression.py", line 201, in main
res = await f
File "/usr/lib/python3.5/asyncio/tasks.py", line 492, in _wait_for_one
return f.result() # May raise f.exception().
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
RuntimeError: cannot reuse already awaited coroutine
Exception ignored in: <bound method BaseEventLoop.__del__ of <_UnixSelectorEventLoop running=False closed=True debug=False>>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/base_events.py", line 431, in __del__
File "/usr/lib/python3.5/asyncio/unix_events.py", line 58, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 139, in remove_signal_handler
File "/usr/lib/python3.5/signal.py", line 47, in signal
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object
Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_UnixSubprocessTransport closed pid=89531 running stdin=<_UnixWritePipeTransport closing fd=8 open>>>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 126, in __del__
File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 101, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 568, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 560, in write_eof
File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
【问题讨论】:
相关understanding-asyncio 【参考方案1】:警告说,此时您调用loop.close()
与仍在运行的进程相关的内容。我想你应该 wait() 终止它(另请阅读链接中的注释)。试试这个:
try:
pcap_run = await asyncio.wait_for(fut, timeout=5)
except asyncio.TimeoutError:
p.terminate()
finally:
await p.wait()
更新:
哦,你可能使用了全局coros
变量:
def get_coros(pcap_list):
coros = [] # <--------------- create new list to fill
for pcap_loc in pcap_list:
for pcap_check in get_pcap_executables():
tmp_coro = (run_check('args'
.format(e=sys.executable, args=args)))
if tmp_coro != False:
coros.append(tmp_coro)
return coros
【讨论】:
我仍然收到相同的错误(RuntimeError: cannot reuse already awaited coroutine),请参阅我的原始帖子我添加了更多日志。也试过 p.communicate() 得到相同的回溯/ @NabaChinde 我在回答中更新了代码 sn-p,你能测试一下它是否可以工作吗? 遇到同样的问题。我认为这里的问题与 TimeOut 无关,因为我没有使用会导致 TimeOut 的数据。我认为这是因为 main 中的第一个 for 循环。请在 main() 中的 for 循环迭代后,在 p_coros 的这个 O/P 下检查我的 main() 中的 p_coros 更新日志作为 我确信问题发生在 for 循环的第二次迭代中的原因是因为当我使用有限数量的 pcap 文件时,for 循环只会在 pcap_list_gen。 i:e 当 len(pcap_list_gen) == 1 时,它不会失败。 @NabaChinde,我更新了答案。我想我们接近解决方案了:)以上是关于ASYNCIO:[错误] 任务被破坏,但它处于挂起状态的主要内容,如果未能解决你的问题,请参考以下文章
Python asyncio/discord.py - 循环退出,任务被破坏,但它处于待处理状态
如何修复'错误:asyncio:任务已被破坏,但它正在等待处理! Python中的错误
Python3 asyncio“任务已被破坏,但处于待处理状态”,具有某些特定条件