从通过套接字对创建的套接字接收消息时可能会丢失数据?

时间:2018-08-20 08:02:58

标签: python sockets python-2.x

以下是我的代码示例,我创建了一个新进程,并使用它通过主进程发送的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在哪里?

1 个答案:

答案 0 :(得分:1)

您可以打印出没有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。