diff options
author | mathieui <mathieui@mathieui.net> | 2021-04-10 11:11:59 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2021-04-11 16:33:53 +0200 |
commit | e49552e4449bc02daa2aa08738a29810dbcd07cb (patch) | |
tree | ddb70c528afcda5a40bf0e3e4b4cfa8f78e127f0 /poezio/asyncio_fix.py | |
parent | 835d7c855a1e5d5ef4f6b02eeeac58ec77072d3b (diff) | |
download | poezio-e49552e4449bc02daa2aa08738a29810dbcd07cb.tar.gz poezio-e49552e4449bc02daa2aa08738a29810dbcd07cb.tar.bz2 poezio-e49552e4449bc02daa2aa08738a29810dbcd07cb.tar.xz poezio-e49552e4449bc02daa2aa08738a29810dbcd07cb.zip |
refactor: rename poezio.asyncio module
no clash with stdlib pls
Diffstat (limited to 'poezio/asyncio_fix.py')
-rw-r--r-- | poezio/asyncio_fix.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/poezio/asyncio_fix.py b/poezio/asyncio_fix.py new file mode 100644 index 00000000..d333ffa6 --- /dev/null +++ b/poezio/asyncio_fix.py @@ -0,0 +1,43 @@ +""" +A module that monkey patches the standard asyncio module to add an +idle_call() method to the main loop. This method is used to execute a +callback whenever the loop is not busy handling anything else. This means +that it is a callback with lower priority than IO, timer, or even +call_soon() ones. These callback are called only once each. +""" + +import asyncio +import functools +import collections +from asyncio import events + +import slixmpp + + +def monkey_patch_asyncio_slixmpp(): + def idle_call(self, callback): + if asyncio.iscoroutinefunction(callback): + raise TypeError("coroutines cannot be used with idle_call()") + handle = events.Handle(callback, [], self) + self._idle.append(handle) + + def my_run_once(self): + if self._idle: + self._ready.append(events.Handle(lambda: None, (), self)) + real_run_once(self) + if self._idle: + handle = self._idle.popleft() + handle._run() + + cls = asyncio.get_event_loop().__class__ + cls._idle = collections.deque() + cls.idle_call = idle_call + real_run_once = cls._run_once + cls._run_once = my_run_once + + spawn_event = slixmpp.xmlstream.XMLStream._spawn_event + + def patchy(self, xml): + self.loop.idle_call(functools.partial(spawn_event, self, xml)) + + slixmpp.xmlstream.XMLStream._spawn_event = patchy |