5

请我需要一些帮助。我正在努力在 Windows 中的两个 python 进程之间发送通知。

我查看了信号模块,但不幸的是,Windows 不支持用户定义的信号。

Windows 使用其他称为消息的东西,但我不知道它是如何工作的或如何在 python 中使用它。如果有人有在 python 中的进程之间发送消息的想法或起点,将不胜感激。

4

3 回答 3

2

这实际上取决于您在寻找什么——您是想要控制消息、非阻塞消息,还是像通常使用signal模块一样捕获外部信号的能力。

由于您想发送“两个 python 进程之间的通知”,我推荐multiprocessing.connection模块的 Client 和 Listener 类,以获得非常简单的面向消息的连接对象对:

http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.connection

在过程 A 中:

listener = Listener(('localhost', 9000)) # local TCP connections on port 9000
remote_conn = listener.accept()
listener.close()
print remote_conn.recv()
# prints 'a pickle-able object'
print remote_conn.recv()
# prints "['another', 'pickle-able', 'object']"

在过程 B 中:

client = Client(('localhost', 9000))
client.send('a pickle-able object')
client.send(['another', 'pickle-able', 'object'])

这些类是内置的这一事实让我很高兴——无需安装!请注意遵循有关安全性和取消腌制数据的文档指南。

于 2013-05-07T22:57:30.017 回答
0

Windows平台上没有SIGUSR1nor SIGUSR2。您可以通过以下方式进行确认:

C:\>python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda custom (64-bit) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> dir(signal)
['CTRL_BREAK_EVENT', 'CTRL_C_EVENT', 'Handlers', 'NSIG', 'SIGABRT', 'SIGBREAK', 'SIGFPE', 'SIGILL', 'SIGINT', 'SIGSEGV', 'SIGTERM', 'SIG_DFL', 'SIG_IGN', 'Signals', '_IntEnum', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_enum_to_int', '_int_to_enum', '_signal', 'default_int_handler', 'getsignal', 'set_wakeup_fd', 'signal']

这里1是来自 Windows 的官方文档。

于 2018-09-25T10:48:25.513 回答
-1

尝试zeromq通过套接字进行进程间通信。

于 2013-05-07T22:25:09.450 回答