Implementation instances of the Python thread pool

Source: Internet
Author: User
Directly on the code:
Copy the Code code as follows:


#-*-Coding:utf-8-*-
Import Queue
Import threading
Import Urllib
Import Urllib2
Import OS

def down (url,n):
print ' Item ' +STR (n) + ' start '
Filename=urllib2.unquote (URL). Decode (' UTF8 '). Split ('/') [-1]
Urllib.urlretrieve (URL, filename)
print ' Item ' +STR (n) + ' finish '


def worker ():
While True:
i = Q.get ()
URL=I[0]
N=I[1]
Down (url,n)
Q.task_done ()


If __name__== "__main__":

num_worker_threads=100

F=open (' Url.txt ')
L=f.readlines ()
Q = Queue.queue ()
For I in Range (num_worker_threads):
t = Threading. Thread (Target=worker)
T.daemon = True
T.start ()

For I in Range (0,len (l)):
Q.put ((l[i],i))

Q.join ()

  • 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.