diff options
author | Alexander Glyzov <bonoba@gmail.com> | 2013-05-31 22:33:27 +0300 |
---|---|---|
committer | Alexander Glyzov <bonoba@gmail.com> | 2013-05-31 22:33:27 +0300 |
commit | e77ca8686a4823b1ab4408c481bed2e3fa44d1c0 (patch) | |
tree | af1404fff4a037a0b87bd909d0f5698768ee3223 /urwid | |
parent | 469e4609ab504a19b3ba02687e7b736079e8e887 (diff) | |
download | urwid-e77ca8686a4823b1ab4408c481bed2e3fa44d1c0.tar.gz |
more robust poll object patching (works on Linux, *BSD & OSX)
Diffstat (limited to 'urwid')
-rwxr-xr-x | urwid/main_loop.py | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/urwid/main_loop.py b/urwid/main_loop.py index 5fdd77f..e0406c0 100755 --- a/urwid/main_loop.py +++ b/urwid/main_loop.py @@ -804,24 +804,33 @@ class TornadoEventLoop(object): #{ _ioloop_registry = WeakKeyDictionary() # {<ioloop> : {<handle> : <idle_func>}} _max_idle_handle = 0 - @classmethod - def _patch_poll(cls, ioloop): #{ - """ Wraps original poll function in the IOLoop's poll object + class PollProxy(object): #{ + """ A simple proxy for a Python's poll object that wraps the .poll() method + in order to detect idle periods and call Urwid callbacks """ - if ioloop in cls._ioloop_registry: - return # we already patched this instance + def __init__(self, poll_obj, idle_map): + self.__poll_obj = poll_obj + self.__idle_map = idle_map - cls._ioloop_registry[ioloop] = idle_map = WeakValueDictionary() - orig_poll = ioloop._impl.poll + def __getattr__(self, name): + return getattr(self.__poll_obj, name) - @wraps(orig_poll) - def new_poll(timeout): + def poll(self, timeout): if timeout >= 0.01: # only trigger idle event if the delay is big enough - for callback in idle_map.values(): + for callback in list(self.__idle_map.values()): callback() - return orig_poll(timeout) + return self.__poll_obj.poll(timeout) + #} - ioloop._impl.poll = new_poll # replace poll() func + @classmethod + def _patch_poll_impl(cls, ioloop): #{ + """ Wraps original poll object in the IOLoop's poll object + """ + if ioloop in cls._ioloop_registry: + return # we already patched this instance + + cls._ioloop_registry[ioloop] = idle_map = WeakValueDictionary() + ioloop._impl = cls.PollProxy(ioloop._impl, idle_map) #} def __init__(self, ioloop=None): #{ @@ -829,7 +838,7 @@ class TornadoEventLoop(object): #{ from tornado.ioloop import IOLoop ioloop = IOLoop.instance() self._ioloop = ioloop - self._patch_poll(ioloop) + self._patch_poll_impl(ioloop) self._watch_handles = {} # {<watch_handle> : <file_descriptor>} self._max_watch_handle = 0 |