From eb8b6a05d75a5abb55205f9859ce6f4223735946 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 23 Nov 2016 10:20:49 +0100 Subject: Clarify event thread handling in the README --- README.md | 8 ++++++++ mpv-test.py | 2 +- mpv.py | 13 ++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b02111f..1468ce4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ player = mpv.MPV(ytdl=True) player.play('https://youtu.be/DOmdB7D-pUU') ``` +Threading +--------- +The ```mpv``` module starts one thread for event handling, since MPV sends events that must be processed quickly. The event queue has a fixed maxmimum size and some operations can cause a large number of events to be sent. + +If you want to handle threading yourself, you can pass ```start_event_thread=False``` to the ```MPV``` constructor and manually call the ```MPV``` object's ```_loop``` function. There is also an out-of-date branch on the repo that you can cherry-pick that brings in asyncio. + +All API functions are thread-safe. If one is not, please file an issue on github. + Advanced Usage ============== ```python diff --git a/mpv-test.py b/mpv-test.py index 14a28a7..f0cabe0 100755 --- a/mpv-test.py +++ b/mpv-test.py @@ -90,7 +90,7 @@ class TestProperties(unittest.TestCase): setattr(self.m, name, 1) setattr(self.m, name, 1.0) setattr(self.m, name, -1.0) - setattr(self.m, name, math.nan) + setattr(self.m, name, float('nan')) elif ptype == str: setattr(self.m, name, 'foo') setattr(self.m, name, '') diff --git a/mpv.py b/mpv.py index de58908..c74e737 100644 --- a/mpv.py +++ b/mpv.py @@ -397,7 +397,7 @@ def _event_loop(event_handle, playback_cond, event_callbacks, message_handlers, class MPV(object): """ See man mpv(1) for the details of the implemented commands. """ - def __init__(self, *extra_mpv_flags, log_handler=None, **extra_mpv_opts): + def __init__(self, *extra_mpv_flags, log_handler=None, start_event_thread=True, **extra_mpv_opts): """ Create an MPV instance. Extra arguments and extra keyword arguments will be passed to mpv as options. """ @@ -419,11 +419,14 @@ class MPV(object): self._key_binding_handlers = {} self._playback_cond = threading.Condition() self._event_handle = _mpv_create_client(self.handle, b'py_event_handler') - loop = partial(_event_loop, self._event_handle, self._playback_cond, self._event_callbacks, + self._loop = partial(_event_loop, self._event_handle, self._playback_cond, self._event_callbacks, self._message_handlers, self._property_handlers, log_handler) - self._event_thread = threading.Thread(target=loop, name='MPVEventHandlerThread') - self._event_thread.setDaemon(True) - self._event_thread.start() + if start_event_thread: + self._event_thread = threading.Thread(target=self._loop, name='MPVEventHandlerThread') + self._event_thread.setDaemon(True) + self._event_thread.start() + else: + self._event_thread = None if log_handler is not None: self.set_loglevel('terminal-default') -- cgit