pyuv 进程执行函数而不是文件

pyuv process executing a function instead of a file

我正在开发一个应用程序,它涉及复制可能很大的文件以响应某些 UDP 请求。我正在使用 pyuv 来监听和处理这些请求,并希望产生一个单独的进程来完成这些复制操作,这样我的主循环就不会被阻止。

目前我正在按以下方式使用 python 的 multiprocessingpyuv 库。

# 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()

其中 self.perform_action 看起来像这样:

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

我正在使用管道获取 return 值 - 还因为我希望在进程完成时触发回调(pipe_read.start_read(self.read_pipe) 在向管道写入内容时分配回调) .

我很乐意用 pyuv.Process (http://pyuv.readthedocs.io/en/v1.x/process.html) 模块代替上面的内容,但是我似乎找不到任何文档或示例来使程序成为目标一个函数而不是一个文件。使用管道感觉有些不对劲,以便在我的进程完成时触发回调并 return ret_val,如果我能让 pyuv.Process 工作并使用它会很棒它的 callback(process_handle, exit_status, term_signal) 回调。

有什么想法吗?这看起来有可能吗?

pyuv 作者在这里 :-) 那是不可能的,libuv 的 uv_spawn 函数会自己创建一个新进程。但是,您可以将处理函数放在不同的 python 文件中并生成它,例如。