Skip to content

Commit e35114c

Browse files
committed
Terminate self when mpv closes by default.
1 parent e81cf36 commit e35114c

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

python_mpv_jsonipc.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
7373

7474
threading.Thread.__init__(self)
7575

76-
def stop(self):
76+
def stop(self, join=True):
7777
"""Terminate the thread."""
7878
if self.socket is not None:
7979
self.socket.close()
80-
self.join()
80+
if join:
81+
self.join()
8182

8283
def send(self, data):
8384
"""Send *data* to the pipe, encoded as JSON."""
@@ -132,12 +133,17 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
132133

133134
threading.Thread.__init__(self)
134135

135-
def stop(self):
136+
def stop(self, join=True):
136137
"""Terminate the thread."""
137138
if self.socket is not None:
138-
self.socket.shutdown(socket.SHUT_WR)
139-
self.socket.close()
140-
self.join()
139+
try:
140+
self.socket.shutdown(socket.SHUT_WR)
141+
self.socket.close()
142+
self.socket = None
143+
except OSError:
144+
pass # Ignore socket close failure.
145+
if join:
146+
self.join()
141147

142148
def send(self, data):
143149
"""Send *data* to the socket, encoded as JSON."""
@@ -267,9 +273,9 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
267273
self.cid_result = {}
268274
self.cid_wait = {}
269275

270-
def stop(self):
276+
def stop(self, join=True):
271277
"""Terminate the underlying connection."""
272-
self.socket.stop()
278+
self.socket.stop(join)
273279

274280
def event_callback(self, data):
275281
"""Internal callback for recieving events from MPV."""
@@ -335,10 +341,10 @@ def put_task(self, func, *args):
335341
"""
336342
self.queue.put((func, args))
337343

338-
def stop(self):
344+
def stop(self, join=True):
339345
"""Terminate the thread."""
340346
self.queue.put("quit")
341-
self.join()
347+
self.join(join)
342348

343349
def run(self):
344350
"""Process socket events. Do not run this directly. Use *start*."""
@@ -381,6 +387,7 @@ def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None,
381387
self.property_bindings = {}
382388
self.mpv_process = None
383389
self.mpv_inter = None
390+
self.quit_callback = quit_callback
384391
self.event_handler = EventHandler()
385392
self.event_handler.start()
386393
if ipc_socket is None:
@@ -402,7 +409,7 @@ def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None,
402409
else:
403410
raise MPVError("MPV process retry limit reached.")
404411

405-
self.mpv_inter = MPVInter(ipc_socket, self._callback, quit_callback)
412+
self.mpv_inter = MPVInter(ipc_socket, self._callback, self._quit_callback)
406413
self.properties = set(x.replace("-", "_") for x in self.command("get_property", "property-list"))
407414
try:
408415
command_list = [x["name"] for x in self.command("get_property", "command-list")]
@@ -437,6 +444,14 @@ def client_message_handler(data):
437444
if len(args) == 2 and args[0] == "custom-bind":
438445
self.event_handler.put_task(self.key_bindings[args[1]])
439446

447+
def _quit_callback(self):
448+
"""
449+
Internal handler for quit events.
450+
"""
451+
if self.quit_callback:
452+
self.quit_callback()
453+
self.terminate(join=False)
454+
440455
def bind_event(self, name, callback):
441456
"""
442457
Bind a callback to an MPV event.
@@ -574,13 +589,13 @@ def play(self, url):
574589
def __del__(self):
575590
self.terminate()
576591

577-
def terminate(self):
592+
def terminate(self, join=True):
578593
"""Terminate the connection to MPV and process (if *start_mpv* is used)."""
579594
if self.mpv_process:
580595
self.mpv_process.stop()
581596
if self.mpv_inter:
582-
self.mpv_inter.stop()
583-
self.event_handler.stop()
597+
self.mpv_inter.stop(join)
598+
self.event_handler.stop(join)
584599

585600
def command(self, command, *args):
586601
"""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy