从正在运行的进程中调用函数

发布于 2024-08-03 23:19:23 字数 101 浏览 2 评论 0原文

我的程序启动一个子进程,它必须在初始化后向父进程发送某种信号。如果我可以在父级中设置一个处理程序,当发送此信号时调用该处理程序,那就完美了。有什么办法可以做到吗?

阿伦迪特

my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?

Alendit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-08-10 23:19:23

如果您使用的是 Python 2.6,则可以使用标准库中的 multiprocessing 模块,特别是管道和队列。文档中的简单示例:

from multiprocessing import Process, Pipe

def f(conn): #This code will be spawned as a new child process
    conn.send([42, None, 'hello']) #The child process sends a msg to the pipe
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,)) # prepare to spawn the child
    p.start() # spawn it
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join() #wait for child to exit

如果您使用的是 Python 2.4 或 2.5,请不要绝望 - 可以使用向后移植 这里

If you are using Python 2.6, you can use the multiprocessing module from the standard library, in particular pipes and queues. Simple example from the docs:

from multiprocessing import Process, Pipe

def f(conn): #This code will be spawned as a new child process
    conn.send([42, None, 'hello']) #The child process sends a msg to the pipe
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,)) # prepare to spawn the child
    p.start() # spawn it
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join() #wait for child to exit

If you are using Python 2.4 or 2.5, don't despair - a backport is available here.

梦归所梦 2024-08-10 23:19:23

父代码:

import signal

def my_callback(signal, frame):
    print "Signal %d received" % (signal,)

signal.signal(signal.SIGUSR1, my_callback)
# start child

子代码:

import os
import signal

signal.kill(os.getppid(), signal.SIGUSR1)

请小心这种形式的 IPC,因为它有其问题,例如:

在最初的 Unix 系统中,当
使用建立的处理程序
signal() 被调用
信号的传递、处置
信号将被重置为
SIG_DFL,而系统没有
阻止传送更多实例
信号的。系统V还提供
signal() 的这些语义。这
信号不好,因为信号可能很差
在处理程序之前再次交付
有机会重建自己。
此外,快速交付
相同的信号可能会导致递归
处理程序的调用。

我建议阅读整个 signal(2) 手册页。

Parent code:

import signal

def my_callback(signal, frame):
    print "Signal %d received" % (signal,)

signal.signal(signal.SIGUSR1, my_callback)
# start child

Child code:

import os
import signal

signal.kill(os.getppid(), signal.SIGUSR1)

Be careful with this form of IPC because it has its issues, e.g.:

In the original Unix systems, when a
handler that was established using
signal() was invoked by the
delivery of a signal, the disposition
of the signal would be reset to
SIG_DFL, and the system did not
block delivery of further instances
of the signal. System V also provides
these semantics for signal(). This
was bad because the signal might be
delivered again before the handler
had a chance to reestablish itself.
Furthermore, rapid deliveries of the
same signal could result in recursive
invocations of the handler.

I recommend reading the whole signal(2) man page.

很快妥协 2024-08-10 23:19:23

您可以使用 Python 标准库中的 信号模块 来注册信号处理程序。然后子进程将使用正常的信号发送机制。

You can use the signal module from the Python standard library to register a signal handler. The subprocess would then use normal signal sending mechanisms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文