我试图创建一个执行两个并行操作的脚本:
Pool
它似乎有效但在所有产品处理之前就停止了。例如,已完成70种产品,但仍有30种产品。
# -*- coding: utf-8 -*-
from datetime import datetime
from multiprocessing.pool import ThreadPool as Pool
from threading import Thread
import requests
RESPONSES = []
POOL_IS_ALIVE = True
with open('products.txt') as f:
LINES = f.readlines()[:100]
def post_request(url):
html = requests.get(url).content
RESPONSES.append(html)
def parse_product(html, url):
# long code which returns instance of class product
def start_requesting(): # Creates a pool with 100 workers
pool = Pool(100)
for n,line in enumerate(LINES):
pool.apply_async(post_request, args=(line[:-1],))
pool.close()
pool.join()
t1 = Thread(target=start_requesting)
def process_responses():
i=0
db = db_manager.db_manager()
while True:
try:
response = RESPONSES.pop()
except IndexError:
continue
product = parse_product(response,'url')
db.insert_product(product)
if not t1.is_alive():
print 'IS_ALIVE NOT'
break
t2 = Thread(target=process_responses)
now = datetime.now()
t1.start()
t2.start()
t2.join() # MAYBE HERE IS THE PROBLEM
t1.join()
print now-datetime.now()
哪里可能是问题?