Ant*_*040 1 python multithreading arguments typeerror
我有这样的事情:
class thread1(threading.Thread):
def __init__(self):
file = open("/home/antoni4040/file.txt", "r")
data = file.read()
num = 1
while True:
if str(num) in data:
clas = ExportToGIMP
clas.update()
num += 1
thread = thread1
thread.start()
thread.join()
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
TypeError: start() takes exactly 1 argument (0 given)
Run Code Online (Sandbox Code Playgroud)
为什么?
thread = thread1需要thread = thread1().否则,您尝试在类上调用方法,而不是类的实际实例.
另外,不要覆盖__init__Thread对象来完成工作 - 覆盖run.
(虽然您可以覆盖__init__以进行设置,但实际上并不在线程中运行,并且还需要调用super().)
以下是代码的外观:
class thread1(threading.Thread):
def run(self):
file = open("/home/antoni4040/file.txt", "r")
data = file.read()
num = 1
while True:
if str(num) in data:
clas = ExportToGIMP
clas.update()
num += 1
thread = thread1()
thread.start()
thread.join()
Run Code Online (Sandbox Code Playgroud)