3

Python 多线程爬虫

 2 years ago
source link: https://fann.im/blog/2010/08/17/python-multi-threading-crawler/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python 多线程爬虫

Aug 17, 2010

队列模块使用步骤(via):

  1. 创建一个 Queue.Queue() 的实例,然后使用数据对它进行填充。
  2. 将经过填充数据的实例传递给线程类,后者是通过继承 threading.Thread 的方式创建的。
  3. 生成守护线程池。
  4. 每次从队列中取出一个项目,并使用该线程中的数据和 run 方法以执行相应的工作。
  5. 在完成这项工作之后,使用 queue.task_done() 函数向任务已经完成的队列发送一个信号。
  6. 对队列执行 join 操作,实际上意味着等到队列为空,再退出主程序。

其中 join() 方法说明:

保持阻塞状态,直到处理了队列中的所有项目为止。在将一个项目添加到该队列时,未完成的任务的总数就会增加。当使用者线程调用 task_done() 以表示检索了该项目、并完成了所有的工作时,那么未完成的任务的总数就会减少。当未完成的任务的总数减少到零时,join() 就会结束阻塞状态。

Was this page helpful?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK