473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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
1 1529
just set timeout while opening a webpage could solve it .e.g.urllib2.urlopen(url, None, 10)
Nov 30 '10 #2

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

Similar topics

1
by: Phillip Parr | last post by:
Is there any way I can impose a time limit on the file() function, say if a file doesn't open within a second I can skip opening it? Thanks...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
2
by: User | last post by:
Anyone know of a function like raw_input(), but with a built-in or user-specified time limit? Ideally, it would return an empty string ('') and resume processing if the user didn't enter something...
0
by: mdanesh | last post by:
I have a VBA macro code that runs a SQL query (that is coppied from MS ACCESS) and it is using record set definition in VBA. It works fine, but it times out before it is done. I believe it is...
9
by: jamesmcd999 | last post by:
I am currently developing a site and noticed that after about 2 minutes, I am being logged out of the members area. I have been trying to find a function that will allow me to specify the time...
1
by: woony | last post by:
I am making a webpage that gets his information from an as400 system. Now I seemed to hit some weird problem. I have for example this select: select field1,field2 from table this is over...
0
by: federico | last post by:
Hello, is there a way to limit the time a "ManagementScope.Connect" execution takes to end before it times out by itself? I tried using ConnectionOptions.TimeOut property to no avail. I am...
6
by: Rafe | last post by:
I tried to post some follow-ups to some issues I posted in the hopes of helping others, but I only get "reply to author" and "forward", but no "reply" option (using GoogleGroups). Is there some...
3
by: harita12 | last post by:
Hi I'm new to c# programming language, I'm making an app regarding online exam through paging, in which I want to show the time limit for the user and it decrease as the time passes & goes to next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.