1

As a part of a project, I am working on a client that sends UDP packets in a certain beat (messages per second) that I take as an input.

My Windows machine can send up to ~30K messages per second, so let's say I need to send 4K messages per second, I can calculate the delay I need to put between each message so I will send approximately the requested amount in each second, and set the delay by using time.sleep().

My problem is that when I get an amount like 27K messages per second, which is really close to the max amount of messages per second, the delay between each message is in micro-seconds, which time.sleep() is not accurate in that unit.
Is there any other way to delay micro-seconds in python?

EDIT- time.sleep() is not accurate in micro-seconds, I need the delay to be accurate

1 Answers1

0

The code below is slightly long, but it works! Just replace 1/1000 with whatever you want!

class Clock:

def __init__(self, fps):
    self.start = perf_counter()
    self.frame_length = 1/fps
@property
def tick(self):
    return int((perf_counter() - self.start)/self.frame_length)

def sleep(self):
    r = self.tick + 1
    while self.tick < r:
        sleep(1/1000)
monkey
  • 526
  • 7
  • 21