Как присоединиться к потоку из другой функцииPython

Программы на Python
Anonymous
Как присоединиться к потоку из другой функции

Сообщение Anonymous »

Мой код выглядит примерно так:
import threading
import time

mode1=False
mode2=True

stop_thread=False

def function1():
while True:
print('function1')

global stop_thread
if stop_thread:
break
def function2():
while True:
print('function2')

global stop_thread
if stop_thread:
break

# t=None
class MyClass():
def run(self):
if mode1:
# global t
t = threading.Thread(target=function1, daemon=True)
t.start()
elif mode2:
# global t
t = threading.Thread(target=function2, daemon=True)
t.start()

def stop(self):
global stop_thread #,t
stop_thread=True
# t.join()

v=MyClass()
v.run()
v.stop()

Как я могу присоединиться к потоку в функции stop()?
Я пытался создать t переменная глобальная, но это не сработало. Если глобально, то возникает такая ошибка:

Cell In[4], line 32
global t
^
SyntaxError: name 't' is used prior to global declaration


Подробнее здесь: https://stackoverflow.com/questions/783 ... r-function

Вернуться в «Python»