By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

image download problem (python)

P: 2
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(timeout),just like this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python                                                           
  2.  
  3. from threading import Thread      
  4. import threading                                              
  5. import time            
  6. import urllib2                                                         
  7.  
  8. class ReaderThread(threading.Thread):                                                     
  9.     def __init__(self):                                                         
  10.         threading.Thread.__init__(self)                                                   
  11.  
  12.     def run(self):         
  13.         print "downloading"    
  14.         try:                                                  
  15.             source = urllib2.urlopen('http://img.taotaosou.cn/look/1/4/971/485839/485839_160.jpg').read()                                                                        
  16.             open('test.jpg', 'w').write(source)                                                          
  17.             print "finished downloading"                                               
  18.         except:
  19.             print '!!!'                                               
  20.  
  21.  
  22. for i in range(100):
  23.     print threading.activeCount()                                                    
  24.     thread = ReaderThread()  
  25.     thread.setDaemon(False)                                                       
  26.     thread.start()                                                                  
  27.     print "joining..."                                                              
  28.     thread.join(5)                                                                 
  29.     print "joined"   
I just hope the threading.activeCount() is less than a MAX_NUM all the time
Nov 28 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
just set timeout while opening a webpage could solve it .e.g.urllib2.urlopen(url, None, 10)
Nov 30 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.