jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
532 stars 67 forks source link

Remote-control of python-mpv across process boundaries #227

Closed maddream-org closed 2 years ago

maddream-org commented 2 years ago

I want to access MPV's loadfile ("play" allias) from another process:

`import multiprocessing

player = mpv.MPV() player.play("...") //It's work

def AnotherProcess(): player.play("...") //It's not work, but i need it.

multiprocessing.Process(target=AnotherProcess).start() `

How can I do that?

jaseg commented 2 years ago

Hi there,

be careful about your terminology: Multiprocessing does not create threads (i.e. execution contexts inisde of a process that share the same memory), but instead creates multiple operating system-level processes that each have their own memory. To communicate commands across process boundaries, you can use the communication utilities from the multiprocessing package. In your case, an easy option would be to use a multiprocessing.Queue. Put 'commands' into the queue on the remote process end, e.g something like queue.put(["play", "/path/to/file"]), and then have a listener running on a separate thread in the local process that reads commands out of that queue and controls mpv.

Note that multiprocessing.Queue is not the same as queue.Queue.