Clarify event thread handling in the README

This commit is contained in:
jaseg 2016-11-23 10:20:49 +01:00
parent efbf182723
commit eb8b6a05d7
3 changed files with 17 additions and 6 deletions

View File

@ -21,6 +21,14 @@ player = mpv.MPV(ytdl=True)
player.play('https://youtu.be/DOmdB7D-pUU') 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 Advanced Usage
============== ==============
```python ```python

View File

@ -90,7 +90,7 @@ class TestProperties(unittest.TestCase):
setattr(self.m, name, 1) setattr(self.m, name, 1)
setattr(self.m, name, 1.0) setattr(self.m, name, 1.0)
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: elif ptype == str:
setattr(self.m, name, 'foo') setattr(self.m, name, 'foo')
setattr(self.m, name, '') setattr(self.m, name, '')

13
mpv.py
View File

@ -397,7 +397,7 @@ def _event_loop(event_handle, playback_cond, event_callbacks, message_handlers,
class MPV(object): class MPV(object):
""" See man mpv(1) for the details of the implemented commands. """ """ 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. """ Create an MPV instance.
Extra arguments and extra keyword arguments will be passed to mpv as options. """ 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._key_binding_handlers = {}
self._playback_cond = threading.Condition() self._playback_cond = threading.Condition()
self._event_handle = _mpv_create_client(self.handle, b'py_event_handler') 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._message_handlers, self._property_handlers, log_handler)
self._event_thread = threading.Thread(target=loop, name='MPVEventHandlerThread') if start_event_thread:
self._event_thread.setDaemon(True) self._event_thread = threading.Thread(target=self._loop, name='MPVEventHandlerThread')
self._event_thread.start() self._event_thread.setDaemon(True)
self._event_thread.start()
else:
self._event_thread = None
if log_handler is not None: if log_handler is not None:
self.set_loglevel('terminal-default') self.set_loglevel('terminal-default')