Python process pool and thread pool

Source: Internet
Author: User

Code

When writing a crawler, performance is consumed primarily in IO requests, which inevitably cause a wait when the URL is requested in single-threaded mode, which slows down the overall request.

ImportRequests fromConcurrent.futuresImportThreadpoolexecutor#Introducing thread pool modulesdefasyns_url (URL):Try: Response=requests.get (URL)exceptException as E:Print('Exception Results', Response.url,response.content)Print('Get Results', Response.url, response.content) url_list={    'http://www.baidu.com',    'http://www/google.com',    'http://dig.chouti.com',    'http://www.bing.com'}pool=threadpoolexecutor (5) forUrlinchurl_list:Print('Start Request', URL) pool.submit (asyns_url,url) pool.shutdown (Wait=true)#terminating a thread

 fromConcurrent.futuresImportProcesspoolexecutorImportRequestsdeffetch_async (URL): Response=requests.get (URL)returnresponseurl_list= ['http://www.github.com','http://www.bing.com']pool= Processpoolexecutor (5) forUrlinchurl_list:pool.submit (fetch_async, URL) pool.shutdown (wait=True)3. Multi-Process Execution
Multi-Process
 fromConcurrent.futuresImportProcesspoolexecutorImportRequestsdeffetch_async (URL): Response=requests.get (URL)returnResponsedefCallback (future):Print(Future.result ()) Url_list= ['http://www.github.com','http://www.bing.com']pool= Processpoolexecutor (5) forUrlinchurl_list:v=pool.submit (fetch_async, URL) v.add_done_callback (callback) Pool.shutdown (Wait=True)3. Multi-process + callback function execution
Multi-process + callback function

Python process pool and thread pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.