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??

Meet Adhia
  • 29
  • 1
  • 4
  • 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 – Lucian Jul 25 '17 at 07:48

3 Answers3

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

James Russo
  • 578
  • 3
  • 18
  • Thanks for sharing some good stuff, but I want to develop a logic on the client side, not the server side. – Meet Adhia Jul 25 '17 at 08:58
  • @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? – James Russo Jul 25 '17 at 15:54
  • Yes, I am constantly sending data to IP and port. I have found a solution to my problem by using Multithreading. – Meet Adhia Jul 26 '17 at 05:55
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)

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
-1
import time   
time.sleep(x)

x : sleep time in seconds

Taranjeet Singh
  • 177
  • 1
  • 3
  • 13