现在的逻辑是这样:
多进程request.posts直到我们得到所需的响应 将事件设置为停止和终止进程
但是,在终止之前执行的请求仍然可以完成其工作,而我负担不起。我需要他们在我的病情完成后立即停止。我什至可以花一秒钟时间杀死脚本的网络适配器,如果那是需要的话。我只需要走正确的路
这是在Windows上运行的python 2.7,Multiprocessing和Requests libs
有什么方法可以在请求中间停止请求模块的连接吗? (需要停止所有它们在不同进程上运行)
例如:
def worker(found_event):
print 'process started'
randDelay = random.randint(4, 10)
response = requests.post('http://httpbin.org/delay'+str(randDelay))
print response.status_code
found_event.set()
if __name__ == "__main__":
found_event = Event()
pool = [Process(target=worker, args=(found_event,)) for i in range(10)]
for p in pool:
p.start()
found_event.wait()
for p in pool:
p.terminate()
for p in pool:
p.join()
有时会返回:
process started
process started
process started
process started
process started
process started
process started
process started
process started
process started
404
有时:
process started
process started
process started
process started
process started
process started
process started
process started
process started
process started
404404