Need some assistance with Python threading/queue

后端 未结 3 621
梦毁少年i
梦毁少年i 2021-02-09 10:26
import threading
import Queue
import urllib2
import time

class ThreadURL(threading.Thread):

    def __init__(self, queue):
        threading.Thread.__init__(self)

            


        
相关标签:
3条回答
  • 2021-02-09 10:29

    One thing, in the thread run function, the while True loop, if some exception happened, the task_done() may not be called however the get() has already been called. Thus the queue.join() may never end.

    0 讨论(0)
  • 2021-02-09 10:46

    looks fine. yann is right about the daemon suggestion. that will fix your hang. my only question is why use the queue at all? you're not doing any cross thread communication, so it seems like you could just send the host info as an arg to ThreadURL init() and drop the queue.

    nothing wrong with it, just wondering.

    0 讨论(0)
  • 2021-02-09 10:53

    Your code looks fine and is quite clean.

    The reason your application still "hangs" is that the worker threads are still running, waiting for the main application to put something in the queue, even though your main thread is finished.

    The simplest way to fix this is to mark the threads as daemons, by doing t.daemon = True before your call to start. This way, the threads will not block the program stopping.

    0 讨论(0)
提交回复
热议问题