Studying multithreading, I wrote a small code that, in theory, should start threads in turn, wrote such a small script:
import queue import threading exit_flag = False class MyThread(threading.Thread): def __init__(self, thread_id, name, q): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name self.q = q def run(self): print("Starting {}".format(self.name)) process_data(self.name, self.q) print("Exiting {}".format(self.name)) def process_data(working_thread, q): while not exit_flag: queue_lock.acquire() if not work_queue.empty(): data = q.get() queue_lock.release() print("{} processing {}".format(working_thread, data)) thread_list = ["Thread-1", "Thread-2", "Thread-3"] name_list = ["One", "Two", "Three", "Four", "Five"] queue_lock = threading.Lock() work_queue = queue.Queue(10) threads = [] t_id = 1 for t_name in thread_list: thread = MyThread(t_id, t_name, work_queue) thread.start() threads.append(thread) t_id += 1 queue_lock.acquire() for word in name_list: work_queue.put(word) queue_lock.release() while not work_queue.empty(): pass exit_flag = True for t in threads: t.join() print("Main thread end!") What is most interesting, while working in the debugging mode, everything works fine, and if you just take it to start, then nothing happens, just the message that the thread is running. Do not tell me where I make a mistake?
process_data()function, then the code block is completely ignored and not executed at all, but if you put everything in ok. And what I want, I want all three threads to perform actions that are in the queue - E1mir