Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 程序退出时消息中忽略异常_Python_Windows_Python Asyncio_Python 3.6 - Fatal编程技术网

Python 程序退出时消息中忽略异常

Python 程序退出时消息中忽略异常,python,windows,python-asyncio,python-3.6,Python,Windows,Python Asyncio,Python 3.6,我最初想在windows上进行异步流通信 from asyncio import * from asyncio.subprocess import PIPE, STDOUT, DEVNULL import sys async def test(exe): inst = await create_subprocess_exec(exe, stdin=PIPE, stdout=PIPE, stderr=STDOUT) inst.stdin.close() inst.wait() #

我最初想在windows上进行异步流通信

from asyncio import *
from asyncio.subprocess import PIPE, STDOUT, DEVNULL
import sys

async def test(exe):
  inst = await create_subprocess_exec(exe, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
  inst.stdin.close()
  inst.wait()

# for subprocess' pipes on Windows
def initialize_async():
  if sys.platform == 'win32':
    set_event_loop(ProactorEventLoop())
  return get_event_loop()

loop = initialize_async()
loop.run_until_complete(test('attrib.exe'))
loop.close()
上面的代码生成以下代码

Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_WindowsSubprocessTransport closed pid=65088 running stdin=<_ProactorWritePipeTransport closed> stdout=<_ProactorReadPipeTransport closing fd=476 read=<_OverlappedFuture cancelled>>>>
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\asyncio\base_subprocess.py", line 132, in __del__
    self.close()
  File "C:\Program Files\Python36\lib\asyncio\base_subprocess.py", line 106, in close
    proto.pipe.close()
  File "C:\Program Files\Python36\lib\asyncio\proactor_events.py", line 84, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 573, in call_soon
    self._check_closed()
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 357, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\asyncio\proactor_events.py", line 95, in __del__
    warnings.warn("unclosed transport %r" % self, ResourceWarning,
  File "C:\Program Files\Python36\lib\asyncio\proactor_events.py", line 54, in __repr__
    info.append('fd=%s' % self._sock.fileno())
  File "C:\Program Files\Python36\lib\asyncio\windows_utils.py", line 152, in fileno
    raise ValueError("I/O operatioon on closed pipe")
ValueError: I/O operatioon on closed pipe
中忽略的异常:
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\Python36\lib\asyncio\base\u subprocess.py”,第132行,在__
self.close()
文件“C:\Program Files\Python36\lib\asyncio\base\u subprocess.py”,第106行,关闭
proto.pipe.close()文件
文件“C:\Program Files\Python36\lib\asyncio\proactor\u events.py”,第84行,关闭
self.\u循环。尽快呼叫(self.\u呼叫\u连接\u丢失,无)
文件“C:\Program Files\Python36\lib\asyncio\base\u events.py”,第573行,即将调用
自我检查关闭()
文件“C:\Program Files\Python36\lib\asyncio\base\u events.py”,第357行,在\u check\u closed
raise RUNTIMERROR('事件循环已关闭')
RuntimeError:事件循环已关闭
在中忽略异常:
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\Python36\lib\asyncio\proactor\u events.py”,第95行,在__
警告。警告(“未关闭的传输%r”%self,ResourceWarning,
文件“C:\Program Files\Python36\lib\asyncio\proactor\u events.py”,第54行,在报告中__
info.append('fd=%s'%self.\u sock.fileno())
文件“C:\Program Files\Python36\lib\asyncio\windows_utils.py”,第152行,文件号
提升值错误(“封闭管道上的I/O操作”)
ValueError:关闭管道上的I/O操作

如何删除此错误?stdin.close和wait似乎不够。

请注意,与同步编程不同,许多
asyncio
函数实际上是应等待的
协程。请参阅如何定义
wait()

您应该修复代码以等待此协同程序:

async def test(exe):
  inst = await create_subprocess_exec(exe, stdin=PIPE, stdout=PIPE, stderr=STDOUT)

  data = await inst.stdout.readline()  # readline, for example, is also coroutine.
  print(data)

  await inst.wait()

您现在不会看到任何错误。

我发现在Linux上替换为
create\u subprocess\u shell
,无论是否存在
wait inst.wait()
。不知道为什么。在终止创建了多个任务的子进程时,在Mac OS上面临同样的问题,即使我同时使用了
terminate()
wait()