Closed qwerty12 closed 1 year ago
The more severe lag issue here.
Ah, reading the reply is slow. I apparently didn't notice before opening this, my apologies.
Since thumbfast currently doesn't actually care about mpv's reply, try this: replace the open_for_reading
line with local open_for_reading = false
. It (hopefully) shouldn't be as bad then.
Ah, it worked but would be more easily freezed than master branch... https://github.com/po5/thumbfast/issues/11
Wow, I never thought of that. Would love to make this the default if we can figure out a way to do it async.
Maybe add_timeout(0) + mp.utils.write_file or mp.utils.append_file, if that gets us real async within lua.
Maybe add_timeout(0) + mp.utils.write_file or mp.utils.append_file, if that gets us real async within lua.
I'm not sure but I think the write would still occur on the same thread the Lua script is being run on, so if that hangs, so does the Lua script.
I've pushed an update that eschews the Lua io
functions in favour of the WinAPI functions through LuaJIT's ffi
module. This allows the pipe handle to be put into non-blocking mode, which is the closest I can get. I've not measured anything, but run
feels pretty quick to return.
If use_lua_io
(should be renamed, I guess) is enabled, this does require that mpv to be built against LuaJIT, but given that shinchiro's Windows builds do so, it's very unlikely a user has a Windows mpv build without LuaJIT, unless it's old or self-compiled. And in that case, the script will silently fallback to the subprocess method.
Another, separate approach: I did have a go at trying my hand at using the WriteFile in Overlapped mode, which is Windows' way of doing async I/O, but I don't like the result:
buf
should probably be kept in the timer callback anywayLGTM. Great performance improvement I got.
On Windows, using
io.open
on the named pipe works and Lua can write to it directly without needing to start an external program. This isn't asynchronous unlikesubprocess
, however, and the write might block the script for too long if the thumbnail process doesn't acknolwedge the request quickly, leading mpv to terminate the script, so this is under a disabled-by-default option. I haven't noticed any issues personally, but if there were, I guess it would be more noticable if trying to thumbnail a non-local video. In my case, I'll take the risk, as starting manycmd
s on my system is a little slow.If a callback is given to
run
, then the pipe is opened for reading too and mpv's reply is stored in thestdout
member of a table similar to onesubprocess
would return to the callback.I couldn't work out how to reuse the handle (assuming it's possible) when I was experimenting with using LuaJIT FFI to call
WriteFile
directly so here it's just closed and opened as necessary. This also unfortunately doesn't work on Linux - the only ways I know to do the same thing there would be to use the LuaSocket or luaposix modules, or if built against it (it is on Arch), LuaJIT's built-inffi
module.