简体   繁体   English

在greenlets / gevent中使用并发。未来。未来

[英]Using concurrent.futures.Future with greenlets/gevent

I have a python library that performs asynchronous network via multicast which may garner replies from other services. 我有一个通过多播执行异步网络的python库,该库可能会收到其他服务的答复。 It hides the dirty work by returning a Future which will capture a reply. 它通过返回将捕获答复的Future来隐藏肮脏的工作。 I am integrating this library into an existing gevent application. 我正在将该库集成到现有的gevent应用程序中。 The call pattern is as simple as: 调用模式很简单:

future = service.broadcast()
# next call blocks the current thread
reply = future.result(some_timeout)

Under the hood, concurrent.futures.Future.result() uses threading.Condition.wait() . 在后台, concurrent.futures.Future.result()使用threading.Condition.wait()

With a monkey-patched threading module, this seems fine and safe, and non-blocking with greenlets. 使用猴子修补的线程模块,这看起来很好且安全,并且不会被greenlets阻塞。

Is there any reason to be worried here or when mixing gevent and concurrent.futures ? 没有任何理由在这里或混合时担心geventconcurrent.futures

Well, as far as I can tell, futures isn't documented to work on top of threading.Condition , and gevent isn't documented to be able to patch futures safely. 好了,至于我可以告诉大家, futures未记录到工作之上threading.Condition ,并gevent未记录到能够修补futures安全。 So, in theory , someone could write a Python implementation that would break gevent . 因此,从理论上讲 ,有人可以编写会破坏gevent的Python实现。

But in practice? 但是实际上呢? It's hard to imagine what such an implementation would look like. 很难想象这样的实现会是什么样子。 You obviously need some kind of sync objects to make a Future work. 您显然需要某种同步对象才能使Future工作。 Sure, you could use an Event , Lock , and Rlock instead of a Condition , but that won't cause a problem for gevent . 当然,您可以使用EventLockRlock代替Condition ,但这不会对gevent造成问题。 The only way an implementation could plausibly break things would be to go directly to the pthreads/Win32/Java/.NET/whatever sync objects instead of using the wrappers in threading . 一个实现有可能破坏事物的唯一方法是直接进入pthreads / Win32 / Java / .NET /任何同步对象,而不是在threading中使用包装器。

How would you deal with that if it happened? 如果发生这种情况,您将如何处理? Well, futures is implemented in pure Python, and it's pretty simple Python, and there's a fully functional backport that works with 2.5+/3.2+. 好吧, futures是用纯Python实现的,它是非常简单的Python,并且有一个功能齐全的backport ,可与2.5 + / 3.2 +一起使用。 So, you'd just have to grab that backport and swap out concurrent.futures for futures . 所以,你只需要抓住的是反向移植和换出concurrent.futuresfutures

So, if you're doing something wacky like deploying a server that's going to run for 5 years unattended and may have its Python repeatedly upgraded underneath it, maybe I'd install the backport now and use that instead. 因此,如果您正在做一些古怪的事情,例如部署一台将在无人看管的情况下运行5年的服务器,并且可能在其下反复升级其Python,那么也许我现在就安装backport并使用它。

Otherwise, I'd just document the assumption (and the workaround in case it's ever broken) in the appropriate place, and then just use the stdlib module. 否则,我将在适当的地方记录假设(以及解决方法,以防万一它被打破),然后使用stdlib模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM