简体   繁体   English

Python asyncore&dbus

[英]Python asyncore & dbus

Is it possible to integrate asyncore with dbus through the same main loop ? 是否可以通过相同的main loopasyncoredbus集成?

Usually, DBus integration is done through glib main loop: is it possible to have either asyncore integrate this main loop or have dbus use asyncore 's ? 通常,DBus集成是通过glib主循环完成的:是否可以让asyncore集成这个主循环让dbus使用asyncore

asyncore sucks. asyncore糟透了。 glib already provides async stuff, so just use glib 's mainloop to do everything. glib已经提供了异步的东西,所以只需使用glib的mainloop来做所有事情。

I wrote a trivial GSource wrapper for one of my own projects called AsyncoreGSource 我为自己的一个名为AsyncoreGSource 的项目编写了一个简单的GSource包装器

Just attach it to an appropriate MainContext : 只需附加到适当的MainContext

source = AsyncoreGSource([socket_map])
source.attach([main_context])

Naturally the defaults are asyncore.socket_map and the default MainContext respectively. 当然,默认值分别是asyncore.socket_map默认的MainContext

You can also try monkey-patching asyncore.socket_map , which would have been my solution had I not poked through the GLib python bindings source code for GSource . 您也可以尝试使用猴子修补asyncore.socket_map ,如果我没有通过GSourceGLib python绑定源代码进行GSource ,那将是我的解决方案。

Although you got what is probably a perfectly reasonable answer, there is another approach - you don't need to use asyncore's loop per se. 虽然你得到的可能是一个非常合理的答案,但还有另一种方法 - 你本身不需要使用asyncore的循环。 Just call asyncore.loop with a zero timeout and a count of 1, which stops it iterating (and thus makes the function name completely misleading) and polls the sockets just once. 只需调用asyncore.loop,其超时为零,计数为1,这将停止迭代(从而使函数名称完全误导)并仅轮询插槽一次。 Call this as often as you need. 根据需要经常调用它。

I don't know anything about glib's async support but if it requires threads you might still get better performance by using asyncore in this way since it will use select or poll and won't need to spawn additional threads. 我对glib的异步支持一无所知,但如果它需要线程,你可能会通过这种方式使用asyncore来获得更好的性能,因为它将使用select或poll而不需要产生额外的线程。

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

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