【发布时间】:2016-07-12 15:48:04
【问题描述】:
我正在编写一个进程(称为请求进程),它会定期发送 HTTP 请求,但可以被另一个线程/进程(称为 主进程)中断随时。最初,我使用线程池处理带有multiprocessing.Event 对象的请求,以等待适当的延迟,然后再发送下一个请求:
# in the main process
poison_pill_event= multiprocessing.Event()
# pass "poison_pill_event" to the requesting process
# in the requesting process which sends requests every wait_interval seconds
poison_pill_event.wait(timeout=time_interval)
这将允许我拥有 time.sleep() 的可中断版本,以防我需要终止该进程。否则,它将允许代码在超时后继续运行。
最近,我发现考虑到我所需的吞吐量和资源,异步设计是更合适的选择,我尝试使用 Tornado。开始使用gen.sleep 作为非阻塞等待,但它不能被中断。
然后切换到使用Toro.Event's wait function 以允许中断。但是,Toro.Event 的 delay 与 multiprocessing.Event 的 timeout 不同之处在于它引发了超时异常,因此会停止执行。另外,我不相信我可以在进程之间共享它,所以我现在正在尝试将我的主进程与请求进程合并,但这应该不会太难。
所以我的问题是如何重新模拟我在 Tornado 中使用 multiprocessing.Event.wait 的行为?
【问题讨论】:
标签: python python-3.x asynchronous concurrency tornado