2

I have developed a code in which, I am reading data from serial port every second. Same data I have to send data to any IP and port but with a specific time interval like 10s,30s etc.

So how to tell socket to go to sleep, that it will not send data every second??

1
  • You cannot actually tell the socket to go to sleep, you can either make a loop with a sleep in it(time.sleep(..)) or a better way to do it is to use a separate thread/pthread with a stack in which you poll data every X seconds and then manipulate it(send it or do whatever you want), this way you don't lock the reading thread Commented Jul 25, 2017 at 7:48

3 Answers 3

3

You do not need to tell the socket to go to sleep, the socket should always be listening. What you could do is have your program polling the socket go to sleep every second like so

import time
while(true):
  sock.recv()
  time.sleep(1)

Or if you would like to be more adventurous you could use an epoll loop which will check to see if your socket has anything received. A good example of epoll loops is here http://scotdoyle.com/python-epoll-howto.html but is most likely not necessary. Just something you may want to look into if you are starting to get into socket programming

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for sharing some good stuff, but I want to develop a logic on the client side, not the server side.
@MeetAdhia epoll loops can be used both on the client and server side. To be honest I think we need a little more information on your problem to properly address it. Are you just constantly sending the same information to the ip and port after you receive it? Does it ever have to change? does the interval change or does it always stay the same?
Yes, I am constantly sending data to IP and port. I have found a solution to my problem by using Multithreading.
0

You won't be able to throttle your socket send rate, the only solution will be to limit the call to your sending socket. First, what you'll have to do here, is to put the received data in a container (take a look to the python Queues), then, you'll have to schedule your sending process. You could use a Timer for this. What it could looks like would be :

class Exchange(object):
    def __init__(self):
        #create the queue
        #create the sockets

    def receive_every_seconds_method(self):
        # Here we put the received data into the queue
        self.the_queue.put(self.receiving_socket.recv())

    def send_data_later(self):
        while not self.the_queue.empty():
            self.emission_socket.send(self.the_queue.get())
        # reschedule
        self.schedule()

    def schedule(self, timeout=30):
        self.timer = Timer(timeout, self.send_data_later)
        self.timer.start()

    def run(self):
        self.schedule(30)
        while self.continue_the_job: #this is the stop condition
            self.receive_every_seconds_method()
            time.sleep(1)

This way, you will be able to send data every 30 seconds (if there are data to send)

Comments

-1
import time   
time.sleep(x)

x : sleep time in seconds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.