Python asyncore &总线
是否可以通过同一个主循环
将asyncore
与dbus
集成?
通常,DBus 集成是通过 glib 主循环完成的:是否可以让 asyncore 集成此主循环或让 dbus 使用 异步的?
Is it possible to integrate asyncore
with dbus
through the same main loop
?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
asyncore
很糟糕。 glib 已经提供了异步功能,因此只需使用 glib 的主循环即可完成所有操作。asyncore
sucks.glib
already provides async stuff, so just useglib
's mainloop to do everything.我写了一个简单的 < code>GSource 我自己的项目之一的包装器 < a href="http://code.google.com/p/lanshare/source/browse/lanshare-gtk.py?spec=svn1d438e64437f3d8039473846a3b9613d183ba3dc&r=1d438e64437f3d8039473846a3b9613d183ba3dc#16" ofollow">
AsyncoreGSource
只是 将其附加到适当的
MainContext
:默认值自然是
asyncore.socket_map
和 默认MainContext
分别。您还可以尝试猴子修补
asyncore.socket_map
< /a>,如果我没有浏览 GLib,这将是我的解决方案GSource
的 python 绑定源代码。I wrote a trivial
GSource
wrapper for one of my own projects calledAsyncoreGSource
Just attach it to an appropriate
MainContext
:Naturally the defaults are
asyncore.socket_map
and the defaultMainContext
respectively.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 forGSource
.尽管您得到的答案可能是完全合理的,但还有另一种方法 - 您不需要使用 asyncore 的循环本身。只需以零超时和计数 1 调用 asyncore.loop,这将停止迭代(从而使函数名称完全误导)并仅轮询套接字一次。根据需要经常拨打此电话。
我对 glib 的异步支持一无所知,但如果它需要线程,您仍然可以通过以这种方式使用 asyncore 获得更好的性能,因为它将使用 select 或 poll 并且不需要生成额外的线程。
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. 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. 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.