本文实例讲述了Python多线程原理与用法。分享给大家供大家参考,具体如下:

先来看个栗子:
下面来看一下I/O秘籍型的线程,举个栗子——爬虫,下面是爬下来的图片用4个线程去写文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import urllib
import threading
import Queue
import timeit
def getHtml(url):
html_page = urllib.urlopen(url).read()
return html_page
# 提取网页中图片的URL
def getUrl(html):
pattern = r'src="(http://img.*?)"' # 正则表达式
imgre = re.compile(pattern)
imglist = re.findall(imgre, html) # re.findall(pattern,string) 在string中寻找所有匹配成功的字符串,以列表形式返回值
return imglist
class getImg(threading.Thread):
def __init__(self, queue, thread_name=0): # 线程公用一个队列
threading.Thread.__init__(self)
self.queue = queue
self.thread_name = thread_name
self.start() # 启动线程
# 使用队列实现进程间通信
def run(self):
global count
while (True):
imgurl = self.queue.get() # 调用队列对象的get()方法从队头删除并返回一个项目
urllib.urlretrieve(imgurl, 'E:\mnt\girls\%s.jpg' % count)
count += 1
if self.queue.empty():
break
self.queue.task_done() # 当使用者线程调用 task_done() 以表示检索了该项目、并完成了所有的工作时,那么未完成的任务的总数就会减少。
imglist = []
def main():
global imglist
url = "http://huaban.com/favorite/beauty/" # 要爬的网页地址
html = getHtml(url)
imglist = getUrl(html)
def main_1():
global count
threads = []
count = 0
queue = Queue.Queue()
# 将所有任务加入队列
for img in imglist:
queue.put(img)
# 多线程爬去图片
for i in range(4):
thread = getImg(queue, i)
threads.append(thread)
# 阻塞线程,直到线程执行完成
for thread in threads:
thread.join()
if __name__ == '__main__':
main()
t = timeit.Timer(main_1)
print t.timeit(1)
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。