Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-thread concurrent fetching by python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "python how to achieve multithreaded concurrent fetching", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to achieve multithreaded concurrent crawling" this article.

Multithreaded concurrent fetching

If a single thread is too slow, you will need multithreading. Here is a simple thread pool template. This program simply prints 1-10, but you can see that it is concurrent.

Although the multithreading of Python is very creepy, it can improve the efficiency to a certain extent for the frequent network type of crawlers.

From threading import Threadfrom Queue import Queuefrom time import sleep# Q is the task queue # NUM is the total number of concurrent threads # JOBS is the number of tasks Q = Queue () NUM = 2JOBS = 1 task specific processing function, responsible for handling a single task def do_somthing_using (arguments): print arguments# this is the worker process Responsible for continuously fetching data from the queue and processing def working (): while True: arguments = q.get () do_somthing_using (arguments) sleep (1) q.task_done () # fork NUM thread waiting queue for i in range (NUM): t = Thread (target=working) t.setDaemon (True) t.start () # queue for i in range (JOBS) : q.put (I) # waiting for all JOBS to complete q.join () these are all the contents of the article "how to achieve multithreaded concurrent fetching by python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report