Python 带子进程的线程
我使用的是Python2.7,是线程的新手。我得到了一个类文件和run方法。但我没有看到在创建线程实例时调用的run方法。我还计划在Python 带子进程的线程,python,multithreading,Python,Multithreading,我使用的是Python2.7,是线程的新手。我得到了一个类文件和run方法。但我没有看到在创建线程实例时调用的run方法。我还计划在run方法中使用subprocess.Popen,为每个文件名获取流程的stdout,并打印输出 请告诉我要调用的run方法缺少什么 class FileScanThread(threading.Thread): def __init__(self, myFileName): print("In File Scan Thread")
run
方法中使用subprocess.Popen
,为每个文件名获取流程的stdout
,并打印输出
请告诉我要调用的run
方法缺少什么
class FileScanThread(threading.Thread):
def __init__(self, myFileName):
print("In File Scan Thread")
self.mapFile = myFileName
#myjar=myFileName
self.start()
def run(self):
print self.mapFile
x= FileScanThread("myfile.txt")
您忘记调用母类构造函数来指定目标。它不是java,
run
没有特别的含义。默认情况下,目标为None
,线程不执行任何操作
import threading
class FileScanThread(threading.Thread):
def __init__(self, myFileName):
threading.Thread.__init__(self,target=self.run)
# another syntax uses "super", which is simpler in python 3
# super().__init__(target=self.run)
print("In File Scan Thread")
self.mapFile = myFileName
#myjar=myFileName
self.start()
def run(self):
print(self.mapFile)
x= FileScanThread("myfile.txt")
x.join() # when you're done
您忘记调用母类构造函数来指定目标。它不是java,
run
没有特别的含义。默认情况下,目标为None
,线程不执行任何操作
import threading
class FileScanThread(threading.Thread):
def __init__(self, myFileName):
threading.Thread.__init__(self,target=self.run)
# another syntax uses "super", which is simpler in python 3
# super().__init__(target=self.run)
print("In File Scan Thread")
self.mapFile = myFileName
#myjar=myFileName
self.start()
def run(self):
print(self.mapFile)
x= FileScanThread("myfile.txt")
x.join() # when you're done
这会满足你的要求。您没有从类线程调用
\uuuuu init\uuuu
class FileScanThread(threading.Thread):
def __init__(self, myFileName):
threading.Thread.__init__(self)
print("In File Scan Thread")
self.mapFile = myFileName
#myjar=myFileName
self.start()
def run(self):
print self.mapFile
x = FileScanThread("myfile.txt")
我认为你不必把目标论点传给它。至少我通常不是这样做的
输出:
In File Scan Thread
myfile.txt
这会满足你的要求。您没有从类线程调用
\uuuuu init\uuuu
class FileScanThread(threading.Thread):
def __init__(self, myFileName):
threading.Thread.__init__(self)
print("In File Scan Thread")
self.mapFile = myFileName
#myjar=myFileName
self.start()
def run(self):
print self.mapFile
x = FileScanThread("myfile.txt")
我认为你不必把目标论点传给它。至少我通常不是这样做的
输出:
In File Scan Thread
myfile.txt
我也尝试过(x.start()),但并没有调用run方法。Python2.7有什么不同的语法吗?我也尝试过(x.start()),但并没有调用run方法。Python2.7是否有不同的语法?是的,如果希望线程运行外部函数,您只想使用
target
。在本例中,类实例就是线程。对,如果希望线程运行外部函数,则只需要使用target
。在本例中,类实例就是线程。