How check if a task is already in python Queue?

后端 未结 12 2447
青春惊慌失措
青春惊慌失措 2020-12-05 15:04

I\'m writing a simple crawler in Python using the threading and Queue modules. I fetch a page, check links and put them into a queue, when a certain thread has finished proc

12条回答
  •  一生所求
    2020-12-05 15:53

    The put method also needs to be overwritten, if not a join call will block forever https://github.com/python/cpython/blob/master/Lib/queue.py#L147

    class UniqueQueue(Queue):
    
        def put(self, item, block=True, timeout=None):
            if item not in self.queue: # fix join bug
                Queue.put(self, item, block, timeout)
    
        def _init(self, maxsize):
            self.queue = set()
    
        def _put(self, item):
            self.queue.add(item)
    
        def _get(self):
            return self.queue.pop()
    

提交回复
热议问题