簡體   English   中英

Python 在 Popen.stdout.readline 上設置超時

[英]Python set timeout on Popen.stdout.readline

我希望能夠在子進程標准輸出上設置超時並在超過超時時返回一個空字符串。

這是我使用 asyncio 這樣做的嘗試。

但是,在asyncio.wait_for使用file.stdout.readline() asyncio.wait_for

知道如何解決這個問題嗎?

import threading
import select
import subprocess
import queue
import time
import asyncio

class collector():
    @staticmethod
    async def readline(file, timeout=3):
        try:
            line = await asyncio.wait_for(file.stdout.readline(), timeout)
        except asyncio.TimeoutError:
            return ""
        else:
            return line

    @staticmethod
    async def background(command):
        f = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        while True:
            line = await collector.readline(file=f, timeout=3)



asyncio.run(collector.background("tail -f /tmp/2222"))

這是調用堆棧:

  File "/tmp/script.py", line 13, in readline
    line = await asyncio.wait_for(file.stdout.readline(), timeout)
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 462, in wait_for
    fut = ensure_future(fut, loop=loop)
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 679, in ensure_future
    raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required

subprocess庫只提供同步功能。 這些不能被asyncio直接使用,手動包裝它們效率低下。

asyncio已經帶有自己的子asyncio 后端 它的流程表示subprocess.Popen類似,但允許協作等待操作。

import asyncio.subprocess

async def background(*command):
    # create subprocess via asyncio
    proc = await asyncio.create_subprocess_exec(
        *command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
    )
    while True:
        line = await readline(proc.stdout, timeout=0.01)
        print("read", len(line), "characters")

# read from an async stream vvvvvvvvvvvvvvvvvv instead of a file-like object
async def readline(stream: asyncio.StreamReader, timeout: float):
    try:
        # stream.readline is a coroutine vvvvvvvvvvvv
        return await asyncio.wait_for(stream.readline(), timeout=timeout)
    except asyncio.TimeoutError:
        return ""


asyncio.run(background("cat", "/dev/random"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM