简体   繁体   English

Python 2进程在套接字上通信

[英]Python 2 process communicate on socket

I need 2 processes to communicate. 我需要2个流程进行交流。 Either may run alone, but when they are running together they should be communicating. 两者都可能单独运行,但是当它们一起运行时,它们应该进行通信。 They only send a few bytes to each other every minute. 他们每分钟只会互相发送几个字节。 Neither is thought of as "host" or "client". 都不认为这是“主机”或“客户端”。

I tried to make a class which handles that in the background. 我试图制作一个在后台处理该类的类。 For instance, it first tries to connect to the socket, if that fails it tries to listen, etc. When one program terminates the other takes over listening, etc. After struggling with this for too long I've given up. 例如,它首先尝试连接到套接字,如果失败,则尝试监听,等等。当一个程序终止时,另一个程序接管监听,等等。在为此苦苦了很长时间之后,我放弃了。 There are far too many bugs and I can't make it work reliably. 有太多的错误,我不能使其可靠地工作。

So I am wondering if there is a good alternate solution, or if there is a robust implementation of this sort of thing I can use. 因此,我想知道是否有一个很好的替代解决方案,或者是否可以使用这种功能的强大实现。 I don't have time to fool around any more. 我没有时间再无所事事了。 I need it to work on windows and linux. 我需要它在Windows和Linux上工作。

使用0mq和两对PUB / SUB套接字。

If you're having problems with sockets, you might want to look at alternative IPC mechanisms. 如果套接字有问题,则可能需要考虑其他IPC机制。 For example, named pipes let two process to communicate as if they were just writing/readig to/from a file. 例如, 命名管道允许两个进程进行通信,就好像它们只是在向文件中写入文件/从文件中读取文件一样。

The following example shows how a named pipe is created and how to open for reading and writing in two different processes: 以下示例显示了如何创建命名管道以及如何在两个不同的过程中打开以进行读取和写入:

import os

pipe_name = '/tmp/ipc'

if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)
    try:
        with open(pipe_name) as f:
            print 'Received:', f.read()
        with open(pipe_name, 'w') as f:
            message = 'Goodbye World!'
            print 'Sending:', message
            f.write(message)
    finally:
        os.remove(pipe_name)
else:
    with open(pipe_name, 'w') as f:
        message = 'Hello World!'
        print 'Sending:', message
        f.write(message)
    with open(pipe_name) as f:
        print 'Received:', f.read()

The first process will: 第一个过程将:

  • Create the named pipe 创建命名管道
  • Receive a message 收到讯息
  • Send another message 传送其他讯息
  • Delete the pipe 删除管道

while the seconde process will just: 而Seconde流程将只是:

  • Send a message 发送一个消息
  • Receive another message 接收其他讯息

If you execute the example above in two different terminals, in the first one you'll get: 如果在两个不同的终端中执行上述示例,则在第一个终端中,您将获得:

Received: Hello World!
Sending: Goodbye World!

and in the second one: 在第二个中:

Sending: Hello World!
Received: Goodbye World!

Note: This is just an example. 注意:这仅是示例。 If you need bidirectional communication it would be more convenient to use two named pipes instead of opening just one for reading/writing when you need to receive/send messages. 如果需要双向通信,则在需要接收/发送消息时,使用两个命名管道而不是仅打开一个管道进行读取/写入会更方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM