mirror of
https://github.com/mpv-player/mpv
synced 2025-05-04 09:10:09 +00:00
python: add mpv.add_timeout function
This commit is contained in:
parent
90e1d6b06e
commit
31fe208d7d
@ -676,7 +676,12 @@ static PyObject *py_mpv_run_event_loop(PyObject *self, PyObject *args)
|
|||||||
Py_DECREF(cctx);
|
Py_DECREF(cctx);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
mpv_event *event = mpv_wait_event(client, -1);
|
// mpv_event *event = mpv_wait_event(client, -1);
|
||||||
|
/**
|
||||||
|
* Do NOT put -1 for it's an io blocking call
|
||||||
|
* let other potential threads survive
|
||||||
|
*/
|
||||||
|
mpv_event *event = mpv_wait_event(client, 0.05);
|
||||||
if (event->event_id == MPV_EVENT_SHUTDOWN) {
|
if (event->event_id == MPV_EVENT_SHUTDOWN) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,6 +8,7 @@ import mpv as _mpv # type: ignore
|
|||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
import typing
|
import typing
|
||||||
|
from threading import Timer
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ class Options:
|
|||||||
return val
|
return val
|
||||||
|
|
||||||
def read_options(self, options: dict, identifier: str,
|
def read_options(self, options: dict, identifier: str,
|
||||||
on_update: typing.Callable[typing.Any, typing.Any]
|
on_update: typing.Optional[typing.Callable[[typing.Any], typing.Any]] = None,
|
||||||
):
|
):
|
||||||
option_types = options.copy()
|
option_types = options.copy()
|
||||||
|
|
||||||
@ -79,14 +80,14 @@ class Options:
|
|||||||
if conffile:
|
if conffile:
|
||||||
mpv.warn("lua-settings/ is deprecated, use directory script-opts/")
|
mpv.warn("lua-settings/ is deprecated, use directory script-opts/")
|
||||||
|
|
||||||
f = conffile and open(conffile, "r")
|
f = conffile and open(conffile)
|
||||||
if not f:
|
if not f:
|
||||||
mpv.debug(f"{conffilename} not found.")
|
mpv.debug(f"{conffilename} not found.")
|
||||||
else:
|
else:
|
||||||
# config exists, read values
|
# config exists, read values
|
||||||
mpv.info("Opened config file " + conffilename + ".")
|
mpv.info("Opened config file " + conffilename + ".")
|
||||||
linecounter = 1
|
linecounter = 1
|
||||||
for line in f.lines():
|
for line in f.readlines():
|
||||||
if line[-1:] == "\r":
|
if line[-1:] == "\r":
|
||||||
line = line[:-1]
|
line = line[:-1]
|
||||||
|
|
||||||
@ -101,7 +102,7 @@ class Options:
|
|||||||
else:
|
else:
|
||||||
convval = self.typeconv(desttypeval, val)
|
convval = self.typeconv(desttypeval, val)
|
||||||
if convval is None:
|
if convval is None:
|
||||||
mpv.error(conffilename+":"+linecounter+
|
mpv.error(conffilename+":"+str(linecounter)+
|
||||||
" error converting value '" + val +
|
" error converting value '" + val +
|
||||||
"' for key '" + key + "'")
|
"' for key '" + key + "'")
|
||||||
else:
|
else:
|
||||||
@ -123,7 +124,7 @@ class Options:
|
|||||||
if (desttypeval := option_types.get(key)) is None:
|
if (desttypeval := option_types.get(key)) is None:
|
||||||
mpv.warn("script-opts: unknown key " + key + ", ignoring")
|
mpv.warn("script-opts: unknown key " + key + ", ignoring")
|
||||||
else:
|
else:
|
||||||
convval = typeconv(desttypeval, val)
|
convval = self.typeconv(desttypeval, val)
|
||||||
if convval is None:
|
if convval is None:
|
||||||
mpv.error("script-opts: error converting value '" + val +
|
mpv.error("script-opts: error converting value '" + val +
|
||||||
"' for key '" + key + "'")
|
"' for key '" + key + "'")
|
||||||
@ -131,14 +132,14 @@ class Options:
|
|||||||
opt[key] = convval
|
opt[key] = convval
|
||||||
|
|
||||||
# initial
|
# initial
|
||||||
parse_opts(mpv.get_property_native("options/script-opts"), options)
|
parse_opts(mpv.get_property_node("options/script-opts"), options)
|
||||||
|
|
||||||
# runtime updates
|
# runtime updates
|
||||||
if on_update:
|
if on_update:
|
||||||
last_opts = options.copy()
|
|
||||||
|
|
||||||
@mpv.observe_property("options/script-opts", mpv.MPV_FORMAT_NODE)
|
@mpv.observe_property("options/script-opts", mpv.MPV_FORMAT_NODE)
|
||||||
def on_option_change(val):
|
def on_option_change(val):
|
||||||
|
last_opts = options.copy()
|
||||||
new_opts = conf_and_default_opts.copy()
|
new_opts = conf_and_default_opts.copy()
|
||||||
parse_opts(val, new_opts)
|
parse_opts(val, new_opts)
|
||||||
changelist = {}
|
changelist = {}
|
||||||
@ -147,8 +148,7 @@ class Options:
|
|||||||
# copy to user
|
# copy to user
|
||||||
options[k] = v
|
options[k] = v
|
||||||
changelist[k] = True
|
changelist[k] = True
|
||||||
last_opts = new_opts
|
if changelist.keys() and on_update is not None:
|
||||||
if changelist.keys():
|
|
||||||
on_update(changelist)
|
on_update(changelist)
|
||||||
|
|
||||||
|
|
||||||
@ -180,6 +180,12 @@ class Mpv:
|
|||||||
observe_properties: dict = {}
|
observe_properties: dict = {}
|
||||||
|
|
||||||
options = Options()
|
options = Options()
|
||||||
|
threads: list = []
|
||||||
|
|
||||||
|
def add_timeout(self, sec, func, *args, **kwargs):
|
||||||
|
t = Timer(sec, func, args=args, kwargs=kwargs)
|
||||||
|
t.start()
|
||||||
|
self.threads.append(t)
|
||||||
|
|
||||||
def get_opt(self, key, default):
|
def get_opt(self, key, default):
|
||||||
return self.get_property_node("options/script-opts").get(key, default)
|
return self.get_property_node("options/script-opts").get(key, default)
|
||||||
@ -488,9 +494,13 @@ class Mpv:
|
|||||||
self.enable_client_message()
|
self.enable_client_message()
|
||||||
self.debug(f"Running {self.name}")
|
self.debug(f"Running {self.name}")
|
||||||
_mpv.run_event_loop(self)
|
_mpv.run_event_loop(self)
|
||||||
|
self.clean_up()
|
||||||
|
|
||||||
def do_clean_up(self):
|
def clean_up(self):
|
||||||
raise NotImplementedError
|
self.clear_timeout()
|
||||||
|
|
||||||
|
def clear_timeout(self):
|
||||||
|
while self.threads:
|
||||||
|
self.threads.pop(0).cancel()
|
||||||
|
|
||||||
mpv = Mpv()
|
mpv = Mpv()
|
||||||
|
Loading…
Reference in New Issue
Block a user