Python 将参数从一个类传递给从threading.Thread继承的另一个类
我的python代码中有两个类。类Python 将参数从一个类传递给从threading.Thread继承的另一个类,python,multithreading,Python,Multithreading,我的python代码中有两个类。类filtero必须向类Man\u Thr发送两个参数,但是如果由构造函数发送,我就不会调用线程metodstart的构造函数。这是我的代码: import threading class Man_Thr(threading.Thread): def __init__(self,cmd,q): self.comando=cmd self.coda=q def run(self): try: proc=subpro
filtero
必须向类Man\u Thr
发送两个参数,但是如果由构造函数发送,我就不会调用线程metodstart
的构造函数。这是我的代码:
import threading
class Man_Thr(threading.Thread):
def __init__(self,cmd,q):
self.comando=cmd
self.coda=q
def run(self):
try:
proc=subprocess.Popen([self.comando],
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.coda.put(proc)
except:
print "Il comando", self.comando, "non esiste. Riprovare."
self.coda.put(-1)
class Filtro(Man_Thr):
def __init__(self,cmd):
q=Queue.Queue()
thr=Man_Thr(cmd,q)
thr.start()
self.result=q.get()
filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1]))
这是我的输出:
Traceback (most recent call last):
File "filtro2.py", line 75, in <module>
filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1]))
File "filtro2.py", line 45, in __init__
thr.start()
File "/usr/lib64/python2.6/threading.py", line 465, in start
raise RuntimeError("thread.__init__() not called")
RuntimeError: thread.__init__() not called
将参数从派生类发送到基类的最佳方法是什么?您需要调用
threading.Thread
的\uuu init\uu
。通常我们使用super
来实现这一点
class Parent(object):
def __init__(self, *args, **kwargs):
print("You've called Parent.__init__")
class Child(Parent):
def __init__(self, child_arg, *args, **kwargs):
print("You've called Child.__init__ with child_arg={}".format(child_arg))
super().__init__(*args, **kwargs)
在您的情况下,您需要执行以下操作:
class Man_Thr(threading.Thread):
def __init__(self, cmd, q, *args, **kwargs):
self.comando = cmd
self.coda = q
super().__init__(*args, **kwargs)
... # the rest of your class as-is
class Filtro(): # why are you inheriting? This isn't a child...
def __init__(self, cmd, *args, **kwargs):
q = queue.Queue()
thr = Man_Thr(cmd, q, *args, **kwargs)
thr.start()
self.result = q.get()
filtro = Filtro(' '.join(sys.argv[1:-1])
一般模式是让每个类接受某些参数,并将任何其他参数传递给其父类的\uuuu init\uuu
方法
我还建议您使用某种参数解析模块,而不是自己处理它。取决于您的Python版本,它位于3.2+的stdlib中
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('outfile', type=argparse.FileType('w'))
parser.add_argument('infile', type=argparse.FileType('r'))
args = parser.parse_args()
for line in args.infile:
print(line.strip())
args.infile.seek(0)
for line in args.infile:
args.outfile.write(line)
你能修复你的缩进并告诉我们你是如何运行代码的吗?我正试图从回溯中把它拼凑起来,但目前没有多大意义。谢谢,我修复了缩进,并说明了如何运行代码。我必须编写一个通过线程运行外部cmd的脚本,将来,我必须编写输出和stderr。从现在起,我必须将两个参数q和cmd发送到类Man_ThrYou还没有编写代码是如何执行的。确实要调用
$pythonfiltro2.py…
,但这在代码中起什么作用?我们这里的代码片段定义了类——如何实例化?方法是:filtro=filtro(''.join(sys.argv[1:len(sys.argv)-1]))
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('outfile', type=argparse.FileType('w'))
parser.add_argument('infile', type=argparse.FileType('r'))
args = parser.parse_args()
for line in args.infile:
print(line.strip())
args.infile.seek(0)
for line in args.infile:
args.outfile.write(line)