diff options
author | Jonathan Slenders <jonathan@slenders.be> | 2016-10-06 21:43:18 +0200 |
---|---|---|
committer | Jonathan Slenders <jonathan@slenders.be> | 2016-10-09 13:20:31 +0200 |
commit | 84d58bb2261b791a4f572a71472e26b20578d778 (patch) | |
tree | bf218c6b71de3e86c50fecb1c621c5d9016033ab | |
parent | c47d5cf4bb258dfc81f74ba612437be03795ab72 (diff) |
Turned the _max_postpone_until argument of call_from_executor into a float. (As returned by `time.time`.)
- This will do less system calls than executing `datetime.datetime.now()`.
- It's backwards-incompatible, but it's a private API anyway. (Used only
by 'pymux'.)
-rw-r--r-- | prompt_toolkit/eventloop/base.py | 6 | ||||
-rw-r--r-- | prompt_toolkit/eventloop/posix.py | 19 | ||||
-rw-r--r-- | prompt_toolkit/interface.py | 17 |
3 files changed, 29 insertions, 13 deletions
diff --git a/prompt_toolkit/eventloop/base.py b/prompt_toolkit/eventloop/base.py index bac1e02d..db86face 100644 --- a/prompt_toolkit/eventloop/base.py +++ b/prompt_toolkit/eventloop/base.py @@ -74,8 +74,12 @@ class EventLoop(with_metaclass(ABCMeta, object)): Call this function in the main event loop. Similar to Twisted's ``callFromThread``. - :param _max_postpone_until: `None` or `datetime` instance. For interal + :param _max_postpone_until: `None` or `time.time` value. For interal use. If the eventloop is saturated, consider this task to be low priority and postpone maximum until this timestamp. (For instance, repaint is done using low priority.) + + Note: In the past, this used to be a datetime.datetime instance, + but apparently, executing `time.time` is more efficient: it + does fewer system calls. (It doesn't read /etc/localtime.) """ diff --git a/prompt_toolkit/eventloop/posix.py b/prompt_toolkit/eventloop/posix.py index de3a78c8..f2db862b 100644 --- a/prompt_toolkit/eventloop/posix.py +++ b/prompt_toolkit/eventloop/posix.py @@ -1,10 +1,10 @@ from __future__ import unicode_literals -import datetime import fcntl import os import random import signal import threading +import time from prompt_toolkit.terminal.vt100_input import InputStream from prompt_toolkit.utils import DummyContext, in_main_thread @@ -20,7 +20,7 @@ __all__ = ( 'PosixEventLoop', ) -_now = datetime.datetime.now +_now = time.time class PosixEventLoop(EventLoop): @@ -125,17 +125,23 @@ class PosixEventLoop(EventLoop): # case. tasks = [] low_priority_tasks = [] - now = _now() + now = None # Lazy load time. (Fewer system calls.) for fd in fds: # For the 'call_from_executor' fd, put each pending # item on either the high or low priority queue. if fd == self._schedule_pipe[0]: for c, max_postpone_until in self._calls_from_executor: - if max_postpone_until is None or max_postpone_until < now: + if max_postpone_until is None: + # Execute now. tasks.append(c) else: - low_priority_tasks.append((c, max_postpone_until)) + # Execute soon, if `max_postpone_until` is in the future. + now = now or _now() + if max_postpone_until < now: + tasks.append(c) + else: + low_priority_tasks.append((c, max_postpone_until)) self._calls_from_executor = [] # Flush all the pipe content. @@ -228,11 +234,12 @@ class PosixEventLoop(EventLoop): Call this function in the main event loop. Similar to Twisted's ``callFromThread``. - :param _max_postpone_until: `None` or `datetime` instance. For interal + :param _max_postpone_until: `None` or `time.time` value. For interal use. If the eventloop is saturated, consider this task to be low priority and postpone maximum until this timestamp. (For instance, repaint is done using low priority.) """ + assert _max_postpone_until is None or isinstance(_max_postpone_until, float) self._calls_from_executor.append((callback, _max_postpone_until)) if self._schedule_pipe: diff --git a/prompt_toolkit/interface.py b/prompt_toolkit/interface.py index c91f3833..f1a7b629 100644 --- a/prompt_toolkit/interface.py +++ b/prompt_toolkit/interface.py @@ -3,7 +3,6 @@ The main `CommandLineInterface` class and logic. """ from __future__ import unicode_literals -import datetime import functools import os import signal @@ -11,6 +10,7 @@ import six import sys import textwrap import threading +import time import types import weakref @@ -326,11 +326,16 @@ class CommandLineInterface(object): self._redraw() # Call redraw in the eventloop (thread safe). - # Give it low priority. If there is other I/O or CPU intensive - # stuff to handle, give that priority, but max postpone x seconds. - _max_postpone_until = datetime.datetime.now() + datetime.timedelta( - seconds=self.max_render_postpone_time) - self.eventloop.call_from_executor(redraw, _max_postpone_until=_max_postpone_until) + # Usually with the high priority, in order to make the application + # feel responsive, but this can be tuned by changing the value of + # `max_render_postpone_time`. + if self.max_render_postpone_time: + _max_postpone_until = time.time() + self.max_render_postpone_time + else: + _max_postpone_until = None + + self.eventloop.call_from_executor( + redraw, _max_postpone_until=_max_postpone_until) # Depracated alias for 'invalidate'. request_redraw = invalidate |