pyuv进程执行函数而不是fi

2024-06-16 14:21:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,其中涉及到复制潜在的大文件,以响应某些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

我使用管道来获取返回值,这也是因为我希望在进程完成时触发回调(pipe_read.start_read(self.read_pipe)在向管道写入内容时分配回调)。你知道吗

我很想用pyuv.Processhttp://pyuv.readthedocs.io/en/v1.x/process.html)模块来代替上面提到的内容,但是我似乎找不到任何文档或示例来让程序以函数而不是文件为目标。当我的进程结束时,使用管道触发回调并返回ret_val会让人感觉不对劲,如果我能让pyuv.Process工作并使用它的callback(process_handle, exit_status, term_signal)回调,那就太好了。你知道吗

有什么想法吗?这似乎有可能吗?你知道吗


Tags: andthetoselfread进程callbackmsg
1条回答
网友
1楼 · 发布于 2024-06-16 14:21:31

pyuv作者:-)这是不可能的,libuv的uv_spawn函数将自己创建一个新进程。但是,您可以将处理函数放在不同的python文件中,并生成相应的python文件。你知道吗

相关问题 更多 >