我要写一个程序,它有多个进程(CPU拥挤)和多线程(IO拥挤)。(下面的代码只是一个示例,而不是程序)
但是当代码遇到join()
时,它会使程序陷入僵局。
我的代码在下面发布
import requests
import time
from multiprocessing import Process, Queue
from multiprocessing.dummy import Pool
start = time.time()
queue = Queue()
rQueue = Queue()
url = 'http://www.bilibili.com/video/av'
for i in xrange(10):
queue.put(url+str(i))
def goURLsCrawl(queue, rQueue):
threadPool = Pool(7)
while not queue.empty():
threadPool.apply_async(urlsCrawl, args=(queue.get(), rQueue))
threadPool.close()
threadPool.join()
print 'end'
def urlsCrawl(url, rQueue):
response = requests.get(url)
rQueue.put(response)
p = Process(target=goURLsCrawl, args=(queue, rQueue))
p.start()
p.join() # join() is here
end = time.time()
print 'totle time %0.4f' % (end-start,)
提前致谢。
答案 0 :(得分:0)
我终于找到了原因。正如您所看到的,我从Queue
导入了multiprocessing
,因此Queue
应仅用于进程,但我会将线程< / strong>访问我的代码上的Queue
,因此必须在程序后面发生未知事件。
要更正它,只需导入Queue
而不是multiprocessing.Queue