diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2018-07-18 09:23:05 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2018-07-26 10:36:16 -0700 |
commit | 0c8d2b2f30aaa1f6dbc7117464836c76597909ed (patch) | |
tree | 76b879ea26ecd0ee788a5c6dc29435a3a6a986e1 /alot/ui.py | |
parent | 065bb9a1077181ee305cdd857dbe2d492850f937 (diff) |
ui: convert the loop_hook to asyncio
asyncio doesn't have a loopingCall helper, so I've implemented a
coroutine that does the same thing, it takes a callable, a period, and
then forwards all other arguments. This is tested and working.
Diffstat (limited to 'alot/ui.py')
-rw-r--r-- | alot/ui.py | 24 |
1 files changed, 17 insertions, 7 deletions
@@ -8,7 +8,7 @@ import codecs import contextlib import asyncio -from twisted.internet import defer, task +from twisted.internet import defer import urwid from .settings.const import settings @@ -25,6 +25,15 @@ from .widgets.globals import CompleteEdit from .widgets.globals import ChoiceWidget +async def periodic(callable_, period, *args, **kwargs): + while True: + try: + callable_(*args, **kwargs) + except Exception as e: + logging.error('error in loop hook %s', str(e)) + await asyncio.sleep(period) + + class UI(object): """ This class integrates all components of alot and offers @@ -110,14 +119,15 @@ class UI(object): unhandled_input=self._unhandled_input, input_filter=self._input_filter) - # Create a defered that calls the loop_hook + loop = asyncio.get_event_loop() + # Create a task for the periodic hook loop_hook = settings.get_hook('loop_hook') if loop_hook: - loop = task.LoopingCall(loop_hook, ui=self) - loop_defered = loop.start(settings.get('periodic_hook_frequency')) - loop_defered.addErrback( - lambda e: logging.error('error in loop hook %s', - e.getErrorMessage())) + # In Python 3.7 a nice aliase `asyncio.create_task` was added + loop.create_task( + periodic( + loop_hook, settings.get('periodic_hook_frequency'), + ui=self)) # set up colours colourmode = int(settings.get('colourmode')) |