首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从通过socketpair创建的套接字接收消息时,可能会丢失数据吗?

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

Stack Overflow用户
提问于 2018-08-20 16:02:58
回答 1查看 21关注 0票数 0

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

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
[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在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 16:25:44

您可以打印子进程在没有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

如果您使用TCP处理上面的字符串,您将得到4,但不会遗漏任何消息,因为sockerpair默认使用TCP。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51926427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档