Skip to content

Instantly share code, notes, and snippets.

@dongguosheng
Last active December 19, 2015 17:19
Show Gist options
  • Save dongguosheng/5990616 to your computer and use it in GitHub Desktop.
Save dongguosheng/5990616 to your computer and use it in GitHub Desktop.
a simple multi-threading in Python
import threading
import urllib
import time
class Mythread(threading.Thread):
'''Download pictures.'''
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
print 'init thread.'
def run(self):
print 'thread running'
print 'begin downloading from %s' % self.url
urllib.urlretrieve(self.url, r'./' + self.url[7 : 12] + '.jpg')
starttime = time.time()
def main():
# some pictures' urls
urllist = ['http://soso7.gtimg.cn/sosopic/0/6704014884959496047/640',
'http://soso1.gtimg.cn/sosopic/0/5484709051946749088/640']
for url in urllist:
mythread = Mythread(url)
mythread.start()
print 'main thread continues.'
# wait for mythread
mythread.join()
print 'main thread waiting for mythread.'
print 'Time : %s' % (time.time() - starttime)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment