AttributeError: 'Thread1'对象没有属性'_initialized'

时间:2021-08-07 18:19:43

well i got this problem when trying to stop a thread with the .join method

当我试图用。join方法停止一个线程时,我遇到了这个问题

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    t1.alto()
  File "C:\Program Files (x86)\Phyton\Thread1.py", line 16, in alto
    t1.join()
AttributeError: 'Thread1' object has no attribute '_initialized'

Here is my Thread1 module:

下面是我的Thread1模块:

import threading

class Thread1(threading.Thread):
    def __init__(self):

        print('Entre al hilo')

    def corre(self):
        x = 10
        for i in range (1, 11,1):
            val = x*i
            print(val, '% completado')

    def alto(self):
        t1.join()

t1 = Thread1()

Finally, this is how I call the Thread1 module:

最后,我这样调用Thread1模块:

if(lista[0] == 'A'):
    self.p1.set('Procesando')
    t1.corre()
    lista.remove('A')
    self.p1.set('Espera')

1 个解决方案

#1


0  

Firstly, you need to call the constructor (init) of the superclass:

首先,需要调用超类的构造函数(init):

class Thread1(threading.Thread):
    def __init__(self):
        super().__init__()
        print('Entre al hilo')
    ...

Secondly, the code you want to run in a separate thread must be put in a method called run:

其次,你想要在一个单独的线程中运行的代码必须放在一个叫做run的方法中:

    def run(self):
        # code to be run in separate thread

Thirdly, to start the separate thread, call start:

第三,要启动单独的线程,调用start:

t1.start()
# the code in run() will now be run in parallell with whatever follows here.

The reason for your error is that you have not actually started the separate thread (with start()), thus there is no thread to join() with.

您错误的原因是您实际上没有启动单独的线程(使用start()),因此没有线程可以连接()。

Also, since you have no run() method, even if you call start() nothing will be run in the thread.

而且,由于没有run()方法,即使调用start()也不会在线程中运行。

#1


0  

Firstly, you need to call the constructor (init) of the superclass:

首先,需要调用超类的构造函数(init):

class Thread1(threading.Thread):
    def __init__(self):
        super().__init__()
        print('Entre al hilo')
    ...

Secondly, the code you want to run in a separate thread must be put in a method called run:

其次,你想要在一个单独的线程中运行的代码必须放在一个叫做run的方法中:

    def run(self):
        # code to be run in separate thread

Thirdly, to start the separate thread, call start:

第三,要启动单独的线程,调用start:

t1.start()
# the code in run() will now be run in parallell with whatever follows here.

The reason for your error is that you have not actually started the separate thread (with start()), thus there is no thread to join() with.

您错误的原因是您实际上没有启动单独的线程(使用start()),因此没有线程可以连接()。

Also, since you have no run() method, even if you call start() nothing will be run in the thread.

而且,由于没有run()方法,即使调用start()也不会在线程中运行。