I am developing an application which involves copying potentially large files in response to certain UDP requests. I am using pyuv to listen and handle these requests, and desire a separate process to be spawned off to complete these copy operations so that my main loop doesn't get held up.
Currently I am using python's multiprocessing
and pyuv
libraries in the following way.
# Get read and write pipes from the OS
r, w = os.pipe()
# Construct pyuv reading pipe and assign callback
pipe_read = pyuv.Pipe(self.loop)
pipe_read.open(r)
pipe_read.start_read(self.read_pipe)
# Construct pyuv writing pipe
pipe_write = pyuv.Pipe(self.loop)
pipe_write.open(w)
# Keep track of pending file operations associated with the read_pipe fileno()
# Allows us to correctly close both pipes
self.pending[pipe_read.fileno()] = (msg.refDataID, msg.refDataSubID, pipe_read, pipe_write, msg.lastEventTime)
# Spawn off a process to operate on the files and write to the pipe
p = Process(target=self.perform_action, args=(pipe_write, src, dest, ))
p.start()
Where self.perform_action
looks something like this:
def perform_action(self, pipe, src, dest):
ret_val = self.copy_function(src, dest)
pipe.write(str(ret_val) # Write the return value to the pipe - triggers the read_pipe callback
I am using pipes to get the return value - and also because I want a callback to fire when the process has completed (pipe_read.start_read(self.read_pipe)
assigns a callback whenever something is written to the pipe).
I would love to subsititute the above out for something that utilizes the pyuv.Process
(http://pyuv.readthedocs.io/en/v1.x/process.html) module, however I cant seem to find any documentation or examples that exist for getting the program to target a function instead of a file. Something just feels wrong using pipes in order to trigger a callback when my process has finished and to return the ret_val
, and it would be great if I could get pyuv.Process
to work and just use its callback(process_handle, exit_status, term_signal)
callback.
Any ideas? Is this something that seems possible?
pyuv author here :-) That's not possible, libuv's uv_spawn
function will create a new process itself. You can, however, put your processing functions in a different python file and spawn that instead, for example.