@@ -73,11 +73,12 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
73
73
74
74
threading .Thread .__init__ (self )
75
75
76
- def stop (self ):
76
+ def stop (self , join = True ):
77
77
"""Terminate the thread."""
78
78
if self .socket is not None :
79
79
self .socket .close ()
80
- self .join ()
80
+ if join :
81
+ self .join ()
81
82
82
83
def send (self , data ):
83
84
"""Send *data* to the pipe, encoded as JSON."""
@@ -132,12 +133,17 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
132
133
133
134
threading .Thread .__init__ (self )
134
135
135
- def stop (self ):
136
+ def stop (self , join = True ):
136
137
"""Terminate the thread."""
137
138
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 ()
141
147
142
148
def send (self , data ):
143
149
"""Send *data* to the socket, encoded as JSON."""
@@ -267,9 +273,9 @@ def __init__(self, ipc_socket, callback=None, quit_callback=None):
267
273
self .cid_result = {}
268
274
self .cid_wait = {}
269
275
270
- def stop (self ):
276
+ def stop (self , join = True ):
271
277
"""Terminate the underlying connection."""
272
- self .socket .stop ()
278
+ self .socket .stop (join )
273
279
274
280
def event_callback (self , data ):
275
281
"""Internal callback for recieving events from MPV."""
@@ -335,10 +341,10 @@ def put_task(self, func, *args):
335
341
"""
336
342
self .queue .put ((func , args ))
337
343
338
- def stop (self ):
344
+ def stop (self , join = True ):
339
345
"""Terminate the thread."""
340
346
self .queue .put ("quit" )
341
- self .join ()
347
+ self .join (join )
342
348
343
349
def run (self ):
344
350
"""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,
381
387
self .property_bindings = {}
382
388
self .mpv_process = None
383
389
self .mpv_inter = None
390
+ self .quit_callback = quit_callback
384
391
self .event_handler = EventHandler ()
385
392
self .event_handler .start ()
386
393
if ipc_socket is None :
@@ -402,7 +409,7 @@ def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None,
402
409
else :
403
410
raise MPVError ("MPV process retry limit reached." )
404
411
405
- self .mpv_inter = MPVInter (ipc_socket , self ._callback , quit_callback )
412
+ self .mpv_inter = MPVInter (ipc_socket , self ._callback , self . _quit_callback )
406
413
self .properties = set (x .replace ("-" , "_" ) for x in self .command ("get_property" , "property-list" ))
407
414
try :
408
415
command_list = [x ["name" ] for x in self .command ("get_property" , "command-list" )]
@@ -437,6 +444,14 @@ def client_message_handler(data):
437
444
if len (args ) == 2 and args [0 ] == "custom-bind" :
438
445
self .event_handler .put_task (self .key_bindings [args [1 ]])
439
446
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
+
440
455
def bind_event (self , name , callback ):
441
456
"""
442
457
Bind a callback to an MPV event.
@@ -574,13 +589,13 @@ def play(self, url):
574
589
def __del__ (self ):
575
590
self .terminate ()
576
591
577
- def terminate (self ):
592
+ def terminate (self , join = True ):
578
593
"""Terminate the connection to MPV and process (if *start_mpv* is used)."""
579
594
if self .mpv_process :
580
595
self .mpv_process .stop ()
581
596
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 )
584
599
585
600
def command (self , command , * args ):
586
601
"""
0 commit comments