使用多处理时在连接中间终止request.post

2024-09-30 10:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

现在的逻辑是:

多进程请求。直到我们得到所需的响应 将事件设置为停止和终止进程

但是,在终止之前执行的请求仍然完成了它们的工作,这是我负担不起的。我需要他们在我的病情结束后立即停止治疗。我甚至可以在一秒钟内为脚本杀死网络适配器,如果需要的话。我只需要走上正轨

这是在Windows上,运行python2.7,多处理和请求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

Tags: ineventfor进程response事件逻辑process

热门问题