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
- #!/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"