【问题标题】:Asyncio stdout - failing异步标准输出 - 失败
【发布时间】:2017-08-15 19:15:15
【问题描述】:

我正在尝试使用 asyncio 来加快调用外部工具来分析多个音频文件的过程。我正在使用 windows、python 3.6(anaconda 安装),我在这里遇到的问题是调用似乎没有等待,或者从未通过标准输出收到结果。

有什么想法吗?

import os
import asyncio

external_tool = r"C:\path\to\external\tool.exe"

def filepaths_generator(root):
    '''
    Simple generator to yield filepaths
    '''
    for path, dirs, files in os.walk(root):
        for f in files:
            yield os.path.join(path,f)


async def async_subprocess_command(*args):
    '''
    A function to run the asyncio subprocess call
    '''
    # Create subprocess
    process = 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()
    # Return stdout
    return stdout.decode().strip()

async def add_external_tool_data_to_dict(filepath):
    external_tool_data = await async_subprocess_command(external_tool,filepath)
    metadata_dict[filepath] = external_tool_data
    return

metadata_dict = {}

loop = asyncio.get_event_loop()
#loop = asyncio.ProactorEventLoop() - Tried this, it doesn't help!

filepaths = filepaths_generator(r"C:\root\path")
tasks = []

for filepath in filepaths:
    #Create tasks in for the loop and add to a list
    task = loop.create_task(add_external_tool_data_to_dict(filepath))
    tasks.append(task)
#Run the tasks
loop.run_until_complete(asyncio.wait(tasks))

loop.close()
print(metadata_dict)

【问题讨论】:

  • 如果我使用 loop = asyncio.ProactorEventLoop() 我得到错误:
  • 文件“C:\Users\danie\Anaconda3\lib\asyncio\base_events.py”,第 340 行,在 _make_subprocess_transport 中引发 NotImplementedError NotImplementedError
  • 在您尝试loop = asyncio.ProactorEventLoop()后,是否跟进:asyncio.set_event_loop(loop)
  • 我没有 - 现在可以了!

标签: python-3.x stdout python-asyncio


【解决方案1】:

对于初学者来说,this:

在 Windows 上,默认的事件循环是 SelectorEventLoop,它不支持子进程。应该改用 ProactorEventLoop

在 Windows 上使用它的示例:

import asyncio, sys

if sys.platform == 'win32':
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)

我会先切换到这个备用事件循环。

【讨论】:

    【解决方案2】:

    根据 Gerrat 的评论,解决方案是使用 ProactorEventLoop 并在其后添加一行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-21
      • 2022-07-23
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多