使用线程守护程序将变量的值从构造函数的方法返回到另一个方法

时间:2021-09-11 20:42:54

I have this part of a script in python:

我在python中有这部分脚本:

class Filtro:
    def __init__(self,cmd):
       def exec_cmd():
            proc = subprocess.Popen([cmd, '-'],
                            stdin=subprocess.PIPE,
                        )
            return proc

       self.thr=threading.Thread(name="Demone_cmd",target=exec_cmd)
       self.thr.setDaemon(True)
       self.proc=self.thr.start()

    def inp(self,txt):
       f=open(txt,"r")
       self.proc.communicate(f.read())
       f.close()


filtro=Filtro(sys.argv[1])
filtro.inp(sys.argv[2])

I want the return value of exec_cmd -- i.e., proc -- in the method inp, but the current code doesn't achieve this -- communication between the methods doesn't work.

我希望exec_cmd的返回值 - 即proc - 在方法inp中,但当前代码没有实现这一点 - 方法之间的通信不起作用。

1 个解决方案

#1


The direct cause of your problem is in self.proc = self.thr.start(): the start() method starts a thread and has no return value. So self.proc is set to None there and self.proc.communicate(f.read()) will cause an exception.

问题的直接原因在于self.proc = self.thr.start():start()方法启动一个线程并且没有返回值。所以self.proc在那里设置为None,self.proc.communicate(f.read())将导致异常。

More generally the use of threads in your snippet seems a bit overkill, subprocess.Popen() in itself already starts a process that runs in parallel with your script, and you can use its communicate() method to send data to the process and retrieve the process results (docs).

更一般地说,在你的代码片段中使用线程似乎有点矫枉过正,subprocess.Popen()本身已经启动了一个与你的脚本并行运行的进程,你可以使用它的communic()方法将数据发送到进程并检索过程结果(docs)。

An important detail using communicate() is to start the process with a pipe for stdout and stderr, else you will not get the process results back. So if you replace your constructor with the following, you should be able to see process results in your inp() method:

使用communic()的一个重要细节是使用stdout和stderr的管道启动进程,否则你将无法获得进程结果。因此,如果使用以下内容替换构造函数,则应该能够在inp()方法中查看过程结果:

def __init__(self,cmd):
    self.proc = subprocess.Popen([cmd, '-'], 
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, 
                                stderr=subprocess.PIPE)

#1


The direct cause of your problem is in self.proc = self.thr.start(): the start() method starts a thread and has no return value. So self.proc is set to None there and self.proc.communicate(f.read()) will cause an exception.

问题的直接原因在于self.proc = self.thr.start():start()方法启动一个线程并且没有返回值。所以self.proc在那里设置为None,self.proc.communicate(f.read())将导致异常。

More generally the use of threads in your snippet seems a bit overkill, subprocess.Popen() in itself already starts a process that runs in parallel with your script, and you can use its communicate() method to send data to the process and retrieve the process results (docs).

更一般地说,在你的代码片段中使用线程似乎有点矫枉过正,subprocess.Popen()本身已经启动了一个与你的脚本并行运行的进程,你可以使用它的communic()方法将数据发送到进程并检索过程结果(docs)。

An important detail using communicate() is to start the process with a pipe for stdout and stderr, else you will not get the process results back. So if you replace your constructor with the following, you should be able to see process results in your inp() method:

使用communic()的一个重要细节是使用stdout和stderr的管道启动进程,否则你将无法获得进程结果。因此,如果使用以下内容替换构造函数,则应该能够在inp()方法中查看过程结果:

def __init__(self,cmd):
    self.proc = subprocess.Popen([cmd, '-'], 
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, 
                                stderr=subprocess.PIPE)