简体   繁体   English

Python:同时使用os.fork(),os.pipe()和subprocess.Popen问题

[英]Python: simultaneous use of os.fork(), os.pipe() and subprocess.Popen issue

Basically, I have the following code segment 基本上,我有以下代码段

#!/usr/bin/env python3
import os
import sys
import subprocess
import time
import shlex
import paho.mqtt.client as paho

if __name__ == "__main__":
    r, w = os.pipe()
    processId = os.fork()
    if processId:
        # wait to ensure tshark is started appropriately
        os.close(w)
        r = os.fdopen(r,'r')
        while True:
             str = r.read()
             print("{} {}".format("Message from child: ",str))
             if str == "ok":
                 break
        print("Parent exit")
    else:
        os.close(r)
        w = os.fdopen(w,'w')
        cmd = "tshark | grep EAP"
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True, universal_newlines=True)
        w.write("ok")
        # send signal to parent stating tshark has started appropriately
        for output in iter(process.stdout.readline, ""):
            pass #if contains EAP packet, do something

I am trying to create a child process that capture packets through tshark , instantly based on the packet captured to perform some action. 我正在尝试创建一个子进程,该进程通过tshark捕获数据包,该过程立即基于捕获的数据包执行一些操作。 I attempted to fork a process so that for the parent process, some action can be done and the child process can capture the packets. 我试图分叉一个进程,以便对于父进程可以执行一些操作,而子进程可以捕获数据包。 I also would like to first ensure child has successfully started tshark before the parent process continue the job. 我还想先确保孩子在父进程继续工作之前已成功启动tshark

Here comes my questions... 我的问题来了...

1) Is the subprocess.Popen 's pipe interfering with the pipe created from os.pipe() ? 1)是subprocess.Popen的管与所创建的管干扰os.pipe()
I was thinking that both were not interfering with each other since they are two distincting sets of pipe. 我当时以为这两个管道是两个截然不同的管道,所以两者互不干扰。 However, I noticed that the parent process cannot receive the message through the read pipe and only if subprocess has some output, the parent will then receive the message. 但是,我注意到父进程无法通过读取管道接收消息,并且仅当subprocess进程具有某些输出时,父进程才会接收消息。 II need some help here... 我需要一些帮助...

2) Is there any approach that the output from subprocess can be capture on a line basis? 2)是否有任何方法可以按行捕获subprocess的输出? I have read this suggested that it is possible to capture output while subprocess is still running, however it only returns the output once the buffer size is reached. 我已经读过这条建议,可以在subprocess仍在运行时捕获输出,但是只有在达到缓冲区大小时它才返回输出。

3) Is it a good or bad approach for mixing os.pipe() , os.fork() and subprocess.Popen all at once? 3)它是一种用于混合好坏的方法os.pipe() os.fork()subprocess.Popen一下子? I am just not sure whether someone will be doing this or there is any better alternatives? 我只是不确定是否有人会这样做,或者还有更好的选择吗? Your help will be highly appreciated :) 非常感谢您的帮助:)

Your str = r.read() is going to read until the pipe closes. 您的str = r.read()将读取直到管道关闭。 Pipes are streaming, not datagrams. 管道正在流式传输,而不是数据报。 Perhaps use .readline() and w.write("ok\\n") , so that it knows when to stop reading data and return? 也许使用.readline()w.write("ok\\n") ,以便它知道何时停止读取数据并返回?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM