Remove monkeypatching hack on the event loop

This allowed us to schedule events in-order later in the event loop, but
was detrimental to using other event loops and debugging.
This commit is contained in:
mathieui 2016-10-05 20:18:51 +02:00
parent 1b5fe57a5e
commit a0a37c19ff
2 changed files with 3 additions and 31 deletions

View File

@ -1,38 +1,10 @@
""" """
A module that monkey patches the standard asyncio module to add an asyncio-related utilities
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 asyncio
from asyncio import events
from functools import wraps from functools import wraps
import collections
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
def future_wrapper(func): def future_wrapper(func):
""" """
Make sure the result of a function call is an asyncio.Future() Make sure the result of a function call is an asyncio.Future()

View File

@ -380,7 +380,7 @@ class XMLStream(asyncio.BaseProtocol):
elif self.xml_depth == 1: elif self.xml_depth == 1:
# A stanza is an XML element that is a direct child of # A stanza is an XML element that is a direct child of
# the root element, hence the check of depth == 1 # the root element, hence the check of depth == 1
self.loop.idle_call(functools.partial(self.__spawn_event, xml)) self._spawn_event(xml)
if self.xml_root is not None: if self.xml_root is not None:
# Keep the root element empty of children to # Keep the root element empty of children to
# save on memory use. # save on memory use.
@ -893,7 +893,7 @@ class XMLStream(asyncio.BaseProtocol):
stanza['lang'] = self.peer_default_lang stanza['lang'] = self.peer_default_lang
return stanza return stanza
def __spawn_event(self, xml): def _spawn_event(self, xml):
""" """
Analyze incoming XML stanzas and convert them into stanza Analyze incoming XML stanzas and convert them into stanza
objects if applicable and queue stream events to be processed objects if applicable and queue stream events to be processed