Python implements multi-thread download for instances based on queue and threading

Source: Internet
Author: User
This article mainly introduces how to download an instance using multiple threads in python based on queue and threading. it is a practical technique, for more information about how to download multiple threads in python based on queue and threading, see the following example. The specific method is as follows:

The main code is as follows:

  #download worker   queue_download = Queue.Queue(0)   DOWNLOAD_WORKERS = 20   for i in range(DOWNLOAD_WORKERS):     DownloadWorker(queue_download).start() #start a download worker        for md5 in MD5S:     queue_download.put(md5)   for i in range(DOWNLOAD_WORKERS):     queue_download.put(None) 

Downloadworkers. py
Class inherits threading. Thread, and the run method is overloaded. threading. Thread. _ init _ (self) is called in _ init ),
Implement time-consuming operations in the run method

Import threading import Queue import md5query import DOM import OS, sys class DownloadWorker (threading. thread): "def _ init _ (self, queue):" "Constructor" "self. _ queue = queue threading. thread. _ init _ (self) def run (self): while 1: md5 = self. _ queue. get () if md5 is None: break # reached end of queue # this is a time-cost produce self. _ down (md5) print "task:", md5, "finished" def _ down (self, md5): config = {'input': sys. stdin, 'output ':'. /samples ', 'Location': 'XXX', 'has-fn': False, 'options': {'connect. timeout': 60, 'timeout': 3600}, 'log': file('logs.txt ', 'w'),} print 'download % s... '% (md5) try: data = downloadproc (config ['location'], config ['options']) # My download process if data: dom, fileData = md5query. splited (data) filename = md5 if config ['has-fn ']: filename =' % s _ % s' % (md5, dom. nodeValue2 ('xxxxxxx ',''). encode ('utf-8') # This is my download method f = file (OS. path. join (config ['output'], filename), 'w') f. write (fileData) f. close () print '% s \ Tok' % (md5) else: print> config ['log'],' % s \ t % s' % (md5, 'failed') failed t Exception, e: print> config ['log'], '% s \ t % s' % (md5, str (e ))

I hope this article will help you with Python programming.

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.