为什么我在Windows上使用异步和等待时未收到ImplementedError?

2022-03-25 00:00:00 python python-asyncio

问题描述

我有以下代码:

import os
import time
import asyncio


async def run_command(*args):
    """
    Example from:
        http://asyncio.readthedocs.io/en/latest/subprocess.html
    """
    # Create subprocess
    process = await asyncio.create_subprocess_exec(
        *args,
        # stdout must a pipe to be accessible as process.stdout
        stdout=asyncio.subprocess.PIPE)

    # Wait for the subprocess to finish
    stdout, stderr = await process.communicate()

    # Result
    result = stdout.decode().strip()

    # Return stdout
    return result


def run_asyncio_commands(tasks):
    """Run tasks using asyncio and return results"""
    loop = asyncio.get_event_loop()
    commands = asyncio.gather(*tasks)  # Unpack list using *
    results = loop.run_until_complete(commands)
    loop.close()
    return results


if __name__ == '__main__':

    start = time.time()

    cmds = [
        ['du', '-sh', '/Users/fredrik/Desktop'],
        ['du', '-sh', '/Users/fredrik'],
        ['du', '-sh', '/Users/fredrik/Pictures']
    ]

    tasks = []
    for cmd in cmds:
        tasks.append(run_command(*cmd))
    results = run_asyncio_commands(tasks)
    print(results)

    end = time.time()
    print('Script ran in', str(end - start), 'seconds')

当我在我的Mac上运行Python 3.6.1中的代码时,我得到这样的结果:

['780K	/Users/fredrik/Desktop', '46G	/Users/fredrik', '52M	/Users/fredrik/Pictures']
Script ran in 6.405519008636475 seconds

但是,当我在Windows上运行相同的脚本时(但是将du命令替换为在Windows上可以工作的命令),同样在Python 3.6.1上,我得到这样的结果:

Traceback (most recent call last):
  File "C:UsersiruserDesktopasynciotest.py", line 66, in <module>
    results = run_asyncio_commands(tasks)
  File "C:UsersiruserDesktopasynciotest.py", line 41, in run_asyncio_commands
    results = loop.run_until_complete(commands)
  File "C:Usersfredrikcondaenvsdev_py36libasyncioase_events.py", line 466, in run_until_complete
    return future.result()
  File "C:UsersiruserDesktopasynciotest.py", line 16, in run_command
    stdout=asyncio.subprocess.PIPE)
  File "C:Usersfredrikcondaenvsdev_py36libasynciosubprocess.py", line 225, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "C:Usersfredrikcondaenvsdev_py36libasyncioase_events.py", line 1190, in subprocess_exec
    bufsize, **kwargs)
  File "C:Usersfredrikcondaenvsdev_py36libasynciocoroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "C:Usersfredrikcondaenvsdev_py36libasyncioase_events.py", line 340, in _make_subprocess_transp
ort
    raise NotImplementedError
NotImplementedError

这是我在windows上用来发unix命令的替身:

cmds = [['C:/Windows/system32/HOSTNAME.EXE']]

Windows版本信息:

Python 3.6.1 | packaged by conda-forge | (default, May 23 2017, 14:21:39) [MSC v.1900 64 bit (AMD64)] on win32
Windows 10 Pro, version 1703, OS build 15063.413

解决方案

不同的事件循环实现方式不同。其中一些有限制(有时与操作系统相关)。默认情况下,Windows使用SelectorEventLoop,您可以在doc中看到:

SelectorEventLoop有以下限制:

  • SelectSelector用于等待套接字事件:支持套接字,最多支持512套接字。
  • loop.add_read()和loop.add_write()仅接受套接字句柄(例如,不支持管道文件描述符)。
  • 不支持管道,因此未实现loop.connect_read_Pipe()和loop.connect_write_Pipe()方法。
  • 不支持子进程,即未实现loop.subprocess_exec()和loop.subprocess_shell()方法。

若要在Windows中运行代码,您可以使用默认情况下可用的备用事件循环-ProactorEventLoop

替换行:

loop = asyncio.get_event_loop()

使用此选项:

loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)

您的代码将正常工作。

相关文章