Checking on a thread / remove from list

前端 未结 5 604
无人及你
无人及你 2020-12-13 19:19

I have a thread which extends Thread. The code looks a little like this;

class MyThread(Thread):
    def run(self):
        # Do stuff

my_threads = []
while         


        
5条回答
  •  甜味超标
    2020-12-13 19:54

    Better way is to use Queue class: http://docs.python.org/library/queue.html

    Look at the good example code in the bottom of documentation page:

    def worker():
        while True:
            item = q.get()
            do_work(item)
            q.task_done()
    
    q = Queue()
    for i in range(num_worker_threads):
         t = Thread(target=worker)
         t.daemon = True
         t.start()
    
    for item in source():
        q.put(item)
    
    q.join()       # block until all tasks are done
    

提交回复
热议问题