Python Forum
Guidance for the basic concept send/receive a heartbeat signal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guidance for the basic concept send/receive a heartbeat signal
#1
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
Reply
#2
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)
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receive Input on Same Line? johnywhy 8 1,045 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,384 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  How can I add an end line character after every value that I receive? GiggsB 11 5,628 Mar-06-2022, 08:51 PM
Last Post: GiggsB
  How to receive webcam capture on spout? buzzdarkyear 2 2,793 Jan-12-2022, 02:26 PM
Last Post: buzzdarkyear
  How we prune Alphabeta Tree Node Using BST concept Anldra12 4 2,535 May-18-2021, 09:17 AM
Last Post: Anldra12
  Your Guidance caslor 1 2,226 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Understanding the concept ( Modules , imports ) erfanakbari1 1 2,278 Nov-25-2019, 01:59 AM
Last Post: Larz60+
  Understand for range concept RavCOder 4 2,890 Oct-29-2019, 02:26 PM
Last Post: newbieAuggie2019
  help with multiprocess concept kiyoshi7 2 2,580 Aug-10-2019, 08:19 PM
Last Post: kiyoshi7
  Receive data from Redis RQ worker process freak14 0 1,981 Jul-15-2019, 12:39 PM
Last Post: freak14

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020