首页 > 解决方案 > 从通过套接字对创建的套接字接收消息时可能会丢失数据?

问题描述

下面是我的代码示例,我分叉了一个新进程并使用它通过主进程发送的 UDS 接收消息,但我time.sleep(5)在子逻辑中添加了一个:

import os
import socket
import threading
import time


def read_content_from_parent_socket(socket):
    while True:
        try:
            result = socket.recv(4096)
            if not result:
                continue
            print("[In threading]: Get response: %s from child." % result)
        except socket.timeout:
            pass


def handle_request_in_method(socket, result):
    print("[In Process]started to execute method, %s. at time: %s" % (result.split(",")[-1], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
    message = ("[In Process]: Response from child at time: %s, %s" % (
    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
    result.split(",")[-1]))
    socket.sendall(message)


def receive_and_reply_to_parent_socket(socket):
    while True:
        try:
            time.sleep(5)
            result = socket.recv(4096)
        except socket.timeout:
            pass
        except Exception:
            pass
        handle_request_in_method(socket, result)


def main():
    # Setup the socket pair
    socket_parent, socket_child = socket.socketpair()
    socket_child.setblocking(True)
    socket_parent.setblocking(True)
    # Listen to the parent socket via thread
    listening_socket = threading.Thread(target=read_content_from_parent_socket, args=(socket_parent, ))
    listening_socket.start()
    p_id = os.fork()
    if p_id == 0:
        socket_parent.close()
        receive_and_reply_to_parent_socket(socket_child)
    # This is in parent process
    # Send Ping recursively every one seconds.
    message_count = 0
    socket_child.close()
    while True:
        time.sleep(1)
        message_count += 1
        message = "[Main process]: Request from parent at time: %s, %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message_count)
        print(message)
        socket_parent.sendall(message)


if __name__ == "__main__":
    main()

然后我们可以发现日志中缺少一些消息:

[Main process]: Request from parent at time: 2018-08-20 15:54:43, 1
[Main process]: Request from parent at time: 2018-08-20 15:54:44, 2
[Main process]: Request from parent at time: 2018-08-20 15:54:45, 3
[Main process]: Request from parent at time: 2018-08-20 15:54:46, 4
[In Process]started to execute method,  4. at time: 2018-08-20 15:54:47
[In threading]: Get response: [In Process]: Response from child at time: 2018-08-20 15:54:47,  4 from child.

为什么会这样?缺少的消息 1、2 和 3 在哪里?

标签: pythonsocketspython-2.x

解决方案


您可以打印出没有收到的子进程split(',')

[Main process]: Request from parent at time: 2018-08-20 16:22:53, 1[Main process]: Request from parent at time: 2018-08-20 16:22:54, 2[Main process]: Request from parent at time: 2018-08-20 16:22:55, 3[Main process]: Request from parent at time: 2018-08-20 16:22:56, 4

如果你用 处理上面的字符串split(',')[-1],你会得到4,但没有消息丢失,因为sockerpair默认使用 TCP。


推荐阅读