Fix MPV.terminate so it can be called from event handlers
This commit is contained in:
parent
667ec6f180
commit
efbf182723
1 changed files with 11 additions and 5 deletions
16
mpv.py
16
mpv.py
|
|
@ -359,6 +359,8 @@ def _event_loop(event_handle, playback_cond, event_callbacks, message_handlers,
|
||||||
try:
|
try:
|
||||||
devent = event.as_dict() # copy data from ctypes
|
devent = event.as_dict() # copy data from ctypes
|
||||||
eid = devent['event_id']
|
eid = devent['event_id']
|
||||||
|
for callback in event_callbacks:
|
||||||
|
callback(devent)
|
||||||
if eid in (MpvEventID.SHUTDOWN, MpvEventID.END_FILE):
|
if eid in (MpvEventID.SHUTDOWN, MpvEventID.END_FILE):
|
||||||
with playback_cond:
|
with playback_cond:
|
||||||
playback_cond.notify_all()
|
playback_cond.notify_all()
|
||||||
|
|
@ -387,8 +389,6 @@ def _event_loop(event_handle, playback_cond, event_callbacks, message_handlers,
|
||||||
target, *args = devent['event']['args']
|
target, *args = devent['event']['args']
|
||||||
if target in message_handlers:
|
if target in message_handlers:
|
||||||
message_handlers[target](*args)
|
message_handlers[target](*args)
|
||||||
for callback in event_callbacks:
|
|
||||||
callback(devent)
|
|
||||||
if eid == MpvEventID.SHUTDOWN:
|
if eid == MpvEventID.SHUTDOWN:
|
||||||
_mpv_detach_destroy(event_handle)
|
_mpv_detach_destroy(event_handle)
|
||||||
return
|
return
|
||||||
|
|
@ -449,9 +449,15 @@ class MPV(object):
|
||||||
|
|
||||||
def terminate(self):
|
def terminate(self):
|
||||||
self.handle, handle = None, self.handle
|
self.handle, handle = None, self.handle
|
||||||
_mpv_terminate_destroy(handle)
|
if threading.current_thread() is self._event_thread:
|
||||||
if self._event_thread:
|
# Handle special case to allow event handle to be detached.
|
||||||
self._event_thread.join()
|
# This is necessary since otherwise the event thread would deadlock itself.
|
||||||
|
grim_reaper = threading.Thread(target=lambda: _mpv_terminate_destroy(handle))
|
||||||
|
grim_reaper.start()
|
||||||
|
else:
|
||||||
|
_mpv_terminate_destroy(handle)
|
||||||
|
if self._event_thread:
|
||||||
|
self._event_thread.join()
|
||||||
|
|
||||||
def set_loglevel(self, level):
|
def set_loglevel(self, level):
|
||||||
_mpv_request_log_messages(self._event_handle, level.encode('utf-8'))
|
_mpv_request_log_messages(self._event_handle, level.encode('utf-8'))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue