Python 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开发者_StackOverflow中文版
's ?
asyncore
sucks. glib
already provides async stuff, so just use glib
's mainloop to do everything.
I wrote a trivial GSource
wrapper for one of my own projects called AsyncoreGSource
Just attach it to an appropriate MainContext
:
source = AsyncoreGSource([socket_map])
source.attach([main_context])
Naturally the defaults are asyncore.socket_map
and the default MainContext
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 for 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. 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.
精彩评论