Issue
Here below is my code sample, I forked a new process and use it to receive the message via UDS sent by main process, but I added a time.sleep(5)
in the child's logic:
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()
then we can find some of the messages are missing from the log:
[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.
Why this is happening? where is the missing message 1, 2 and 3?
Solution
you could print out what child process received without 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
if you process the string above with split(',')[-1]
, you get 4
, but there is no message missed, because sockerpair
use TCP by default.
Answered By - georgexsh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.