Home  >  Q&A  >  body text

python - 如何解决queue中同一个参数被多个线程同时调用?

本人想实现一个基础的多线程爬虫,初步想法是筛选目录中多个电影标题是否有关键字,没有的话就进入到详情页中查找电影类别中是否有关键字。
下面是一部分代码:

executor = ThreadPoolExecutor(max_workers=5)  #Future模块的多线程
executor.map(get_page,whole)   #get_page()为筛选标题的函数
time.sleep(5)
while not que.empty():
    que_data = que.get()
    filter(que_data)    #filter()为筛选分类关键字的函数,但每次五个线程同时访问同一个url

但现在每次多个线程同时调用queue中的同一个url,请问有什么解决办法吗

天蓬老师天蓬老师2741 days ago705

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-18 10:30:48

    1. Dedicate a thread to read the url in the queue, and then assign it to a thread

    2. Lock access to this queue

    reply
    0
  • Cancelreply