Python生产者/消费者无限期阻塞
我似乎不明白为什么基于队列的生产者/消费者进程会无限期地阻塞和执行:Python生产者/消费者无限期阻塞,python,multithreading,producer-consumer,Python,Multithreading,Producer Consumer,我似乎不明白为什么基于队列的生产者/消费者进程会无限期地阻塞和执行: def producer(q,urls): for url in urls: thread = ThreadChild(Profile.collection,Profile.collection,url,True) thread.follow_links = follow thread.start() q.p
def producer(q,urls):
for url in urls:
thread = ThreadChild(Profile.collection,Profile.collection,url,True)
thread.follow_links = follow
thread.start()
q.put(thread,True)
log.info('Done making threads')
def consumer(q,total_urls):
thread = q.get(True)
thread.join(timeout=40.0)
q.task_done()
q = Queue(2)
prod_thread = threading.Thread(target=producer, args=(q, urls))
cons_thread = threading.Thread(target=consumer, args=(q, len(urls)))
prod_thread.start()
cons_thread.start()
prod_thread.join(timeout=60.0)
cons_thread.join(timeout=60.0)
我已经尝试在生产者线程和消费者线程以及生产者生成的子进程上超时,并且该进程仍然无限期地在on上运行
ThreadChild
是一个进程,它执行一些简单的网络作业,直到要处理的URL用完为止。线程的执行时间不应该太长。varurl
只是线程要处理的URL列表。值得注意的是,“完成生成线程”的日志从未打印过(log
是绑定到StreamHandler
的std python记录器)
为生产者线程和消费者线程定义的超时是否应该在60秒后终止所有内容,而不管队列中剩下什么?我是否误解了这些方法的使用,并将添加到队列中的内容结构化为错误的方式?缺少完整的列表,很难分辨出什么是错误的。您应该检查以下几点:1)ThreadChild类是否可以独立运行而不出现故障?2) 关于Profile类的相同检查。3) 您是否在定义生产者和消费者函数之前定义了ThreadChild?4) Queue的task_done函数会抛出错误吗?如果我用一个永不终止的线程替换ThreadChild,一切都会按照您的预期进行(即所有线程都会被创建并最终超时)。您是否跟踪到了生产者实际执行的是什么?为什么要在队列中传递对线程本身的引用,而不是它生成的消息?