我下面的例程获取urllib2.Requests的列表,并为每个请求生成一个新进程,然后将它们触发。其目的是为了实现异步速度,所以这一切都是一劳永逸(不需要响应)。问题是下面代码中产生的进程永远不会终止。所以在吃了几杯之后,盒子就会砰的一声。上下文:Django web应用程序。有什么帮助吗?在
MP_CONCURRENT = int(multiprocessing.cpu_count()) * 2
if MP_CONCURRENT < 2: MP_CONCURRENT = 2
MPQ = multiprocessing.JoinableQueue(MP_CONCURRENT)
def request_manager(req_list):
try:
# put request list in the queue
for req in req_list:
MPQ.put(req)
# call processes on queue
worker = multiprocessing.Process(target=process_request, args=(MPQ,))
worker.daemon = True
worker.start()
# move on after queue is empty
MPQ.join()
except Exception, e:
logging.error(traceback.print_exc())
# prcoess requests in queue
def process_request(MPQ):
try:
while True:
req = MPQ.get()
dr = urllib2.urlopen(req)
MPQ.task_done()
except Exception, e:
logging.error(traceback.print_exc())
这比你的代码快5-10倍
也许我不对,但是
将side“brocker”集成到django(例如
rabbitmq
或类似的东西)。在相关问题 更多 >
编程相关推荐