Add cleanup in MPV class and shorthand command method in MPVControl
This commit is contained in:
parent
694f7aab11
commit
4f7aebd6fb
@ -6,7 +6,7 @@ from . import nyasync
|
|||||||
mpv_control = mpv.MPVControl()
|
mpv_control = mpv.MPVControl()
|
||||||
|
|
||||||
async def test():
|
async def test():
|
||||||
await mpv_control.send_request({"command":["loadfile",'grzegorz/res/logo.jpg']})
|
await mpv_control.loadfile('grzegorz/res/logo.jpg')
|
||||||
|
|
||||||
async def entry():
|
async def entry():
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -33,17 +34,24 @@ class MPV:
|
|||||||
else:
|
else:
|
||||||
raise Exception("MPV died before socket connected")
|
raise Exception("MPV died before socket connected")
|
||||||
|
|
||||||
await asyncio.gather(
|
self._future = asyncio.gather(
|
||||||
self.ensure_running(),
|
self.ensure_running(),
|
||||||
self.process_outgoing(),
|
self.process_outgoing(),
|
||||||
self.process_incomming(),
|
self.process_incomming(),
|
||||||
)
|
)
|
||||||
|
await self._future
|
||||||
|
|
||||||
|
def _cleanup(self):
|
||||||
|
if os.path.exists(self._ipc_endpoint):
|
||||||
|
os.remove(self._ipc_endpoint)
|
||||||
|
self._future.cancel()#reduces a lot of errors on exit
|
||||||
|
|
||||||
def is_running(self):
|
def is_running(self):
|
||||||
return self.proc.returncode is None
|
return self.proc.returncode is None
|
||||||
|
|
||||||
async def ensure_running(self):
|
async def ensure_running(self):
|
||||||
await self.proc.wait()
|
await self.proc.wait()
|
||||||
|
self._cleanup()
|
||||||
raise Exception("MPV died unexpectedly")
|
raise Exception("MPV died unexpectedly")
|
||||||
|
|
||||||
async def process_outgoing(self):
|
async def process_outgoing(self):
|
||||||
@ -81,3 +89,8 @@ class MPVControl:
|
|||||||
# is the safest option.
|
# is the safest option.
|
||||||
self.mpv.requests.put_nowait(msg)
|
self.mpv.requests.put_nowait(msg)
|
||||||
return await self.mpv.responses.get()
|
return await self.mpv.responses.get()
|
||||||
|
|
||||||
|
#Shorthand command requests:
|
||||||
|
async def loadfile(self, file):
|
||||||
|
return await self.send_request({"command":["loadfile", file]})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user