image download problem (python)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zyy max
    New Member
    • Nov 2010
    • 2

    image download problem (python)

    I was trying to download images with multi-thread, which has a limited max_count in python.

    Each time a download_thread is started, I leave it alone and activate another one. I hope the download process could be ended in 5s, which means downloading is failed if opening the url costs more than 5s.

    But how can I know it and stop the failed thread??? Thanks for your help!!!
    I tried to use thread.join(tim eout),just like this:

    Code:
    #!/usr/bin/env python                                                           
                                                                                    
    from threading import Thread      
    import threading                                              
    import time            
    import urllib2                                                         
                                                                                    
    class ReaderThread(threading.Thread):                                                     
        def __init__(self):                                                         
            threading.Thread.__init__(self)                                                   
                                                                                    
        def run(self):         
            print "downloading"    
            try:                                                  
                source = urllib2.urlopen('http://img.taotaosou.cn/look/1/4/971/485839/485839_160.jpg').read()                                                                        
                open('test.jpg', 'w').write(source)                                                          
                print "finished downloading"                                               
            except:
                print '!!!'                                               
                                       
                             
    for i in range(100):
        print threading.activeCount()                                                    
        thread = ReaderThread()  
        thread.setDaemon(False)                                                       
        thread.start()                                                                  
        print "joining..."                                                              
        thread.join(5)                                                                 
        print "joined"
    I just hope the threading.activ eCount() is less than a MAX_NUM all the time
  • zyy max
    New Member
    • Nov 2010
    • 2

    #2
    just set timeout while opening a webpage could solve it .e.g.urllib2.ur lopen(url, None, 10)

    Comment

    Working...