以生产者和消费者为例,说明线程间通信
Queue是线程间安全类, queue.Queue
类内部使用一个列表(或其他容器)来存储项目,并使用一个互斥锁(mutex
)来保护数据结构的完整性。
#! /usr/bin/python
#coding=utf-8
# filename: downloadfile.py
import os,urllib.request,re
from multiprocessing import Queue
import threading
q = Queue()
bover = False
def opensrc():
fo = open('source.csv')
line = fo.readline()
while line:
line = fo.readline()
line = line[:-1]
q.put(line)
fo.close()
bover = True
def worker():
while(True):
line = q.get(block=False)
if line == '' and bover == True:
break
if line == '':
continue
strs = line.split(',')
conno = strs[1]
conno = conno[1:-1]
filename = strs[3]
filename = filename[1:-1]
filename = conno+'_'+filename
print(filename)
url = strs[4]
url = url[1:-1]
print(url)
data = urllib.request.urlopen(url).read()
with open(filename, 'wb') as fw:
fw.write(data)
print('download',filename,' ok.')
fw.close()
if __name__ == '__main__':
rt = threading.Thread(target=opensrc)
rt.start()
rt.join()
wts = []
for i in range(100):
wts.append(threading.Thread(target=worker))
wts[i].start()
for i in range(100):
wts[i].join()
print("download all.")