python - 从通过套接字对创建的套接字接收消息时可能会丢失数据?
问题描述
下面是我的代码示例,我分叉了一个新进程并使用它通过主进程发送的 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 在哪里?
解决方案
您可以打印出没有收到的子进程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。
推荐阅读
- c++ - 排序算法和对象指针的问题
- javascript - Why is non-reachable part of ternary operator evaluated before state update via setTimeout?
- apache-kafka - Does Kafka stand alone connector syncs automatically or it needs restart every time
- java - android.database.sqlite.SQLiteException:没有这样的列:名称(代码 1)
- html - 如何在 HTML 的特定区域中阻止右键单击?
- node.js - 如何动态渲染快速视图
- php - PHP Mongodb find() 没有按预期工作
- python - 如何合并原始中两个不同列表中的两个子列表
- c - 播放停止时 ALSA PCM 点击/弹出
- javascript - 如何使用 JavaScript 模糊背景几秒钟?