![]() |
Guidance for the basic concept send/receive a heartbeat signal - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Guidance for the basic concept send/receive a heartbeat signal (/thread-42274.html) |
Guidance for the basic concept send/receive a heartbeat signal - StefanL38 - Jun-09-2024 Hi Everybody, I have some experience in programming pascal / delphi and C++. So this means I know the basic concepts. But I have almost no knoweldge about python its functions and the syntax. I would like to have some advice for the basic concept. I want to write code with this functionality: Computer A sends a heartbeat signal over network once every minute towards a computer B. Computer B is waiting to receive this heartbeat-signalfrom Computer A. Whenever the heartbeat-signal is received nothing else shall happen. If no heartbeat-signal is received by computer B within one minute the python-code running on computer B shall start to count up count = 1 2 minutes no heartbeat-signal count = 2 3 minutes no heartbeat-signal count = 3 4 minutes no heartbeat-signal count = 4 5 minutes no heartbeat-signal count = 5 if variable count has reached value 5 computer B shall execute code that sends an email. This email shall only be sent a single time. After sending the email one single time computer B shall just wait for heartbeat-signals and do nothing else If a new heartbeat-signal is received by computer B The code running on computer B shall start checking for following up heartbeat-signals the same way as described above If computer B does not receive new heartbeat-signals the python-code running on computer B shall start to count up count = 1 2 minutes no heartbeat-signal count = 2 3 minutes no heartbeat-signal count = 3 4 minutes no heartbeat-signal count = 4 5 minutes no heartbeat-signal count = 5 and this cycle repeats. As I am very new to python I don't know what basic concept to choose to obtain this functionality with rather simple code than high sophisticated code that is hard to understand basic ideas that I have: send a short UDP-message from computer A to computer B acting on th ereceived UDP-message. I have made some initial steps by send and receive UDP-messages but with the receiver-code that I have found online the code is blocked inside the line data,addr = sock.recvfrom(1024) #receive data from client So I can't check if the heartbeat-signal received within a minute I made a try to use a child-thread but have problems to use variables between the threads I tried saving the UDP-message into a textfile and have another code looking onto the harddisk if this file is present which lead to access-denied errors So this is why I am asking here what basic concept to use will it be easier to use global variables in threads or does this not work at all and I should use this "queing mechanism ? would it be easier to use some kind of try -except construction ? You may find try - except "ugly" coding. As this is only about a heartbeat-signal a few bytes once per minute I don't care about the computational power this needs What do you suggest as the basic concept? If you could mention at least some keywords so I can lookup these keywords for code-examples this would be sufficient. Any code-examples that go into the right direction are welcomed of course best regards Stefan RE: Guidance for the basic concept send/receive a heartbeat signal - Gribouillis - Jun-09-2024 You could start a thread that reads from the socket and fills a queue.Queue input_queue = queue.Queue() class ReaderThread(Thread): def run(self): ... # create socket while True: item = sock.recvfrom(1024) input_queue.put(item)Then you start the thread and you read from the queue with a timeout reader = ReaderThread() reader.start() while True: for no_heartbeat_count in range(5): try: data, addr = input_queue.get(timeout=60) except queue.Empty: pass else: input_queue.task_done() break else: send_email() continue process_item(data, addr) |