import threading
import time
class test(object):
def __init__(self):
self.array = []
def timer(self):
for i in range(100):
self.array.append(f"id {threading.get_ident()} - {i}")
time.sleep(5)
def main(self):
while True:
time.sleep(0.1)
response = str(input(">>> ")).lower()
if response == "start":
thr = threading.Thread(target=self.timer)
thr.start()
elif response == "get":
print(self.array)
elif response == "threads":
for thread in threading.enumerate():
print(thread.name)
if __name__ == "__main__":
my_thread = threading.Thread(target=test().main)
my_thread.start()
Вывод из консоли:
>>> start
>>> start
>>> threads
MainThread
Thread-1
Thread-2
Thread-3
>>>
Знаю, что в multiprocessing можно убить поток, но долго будет переписывать проект.