Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -- time.sleep() offset by code duration

Tags:

python

time

mud

I have a function that runs a tick() for all players and objects within my game server. I do this by looping through a set every .1 seconds. I need it to be a solid .1. Lots of timing and math depends on this pause being as exact as possible to .1 seconds. To achieve this, I added this to the tick thread:

start_time = time.time()

# loops and code and stuff for tick thread in here...

time_lapsed = time.time() - start_time # get the time it took to run the above code
if 0.1 - time_lapsed > 0:
    time.sleep(0.1 - time_lapsed)
else:
    print "Server is overloaded!"
    # server lag is greater that .1, so don't sleep, and just eat it on this run. 
    # the goal is to never see this.

My question is, is this the best way to do this? If the duration of my loop is 0.01, then time_lapsed == 0.01 ... and then the sleep should only be for 0.09. I ask, because it doesn't seem to be working. I started getting the overloaded server message the other day, and the server was most definitely not overloaded. Any thoughts on a good way to "dynamically" control the sleep? Maybe there's a different way to run code every tenth of a second without sleeping?

like image 545
jtsmith1287 Avatar asked Nov 08 '12 17:11

jtsmith1287


People also ask

How do you sleep 5 seconds in Python?

Adding a Python sleep() Call With time.sleep() Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

What does time sleep () do in Python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

Does time sleep stop all code?

Yes, time. sleep will halt your program.

What is the alternative for time sleep in Python?

The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below: Example: A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started.


1 Answers

It would be better to base your "timing and math" on the amount of time actually passed since the last tick(). Depending on "very exact" timings will be fragile at the best of times.

Update: what I mean is that your tick() method would take an argument, say "t", of the elapsed time since the last call. Then, to do movement you'd store each thing's position (say in pixels) and velocity (in "pixels/second") so the magnitude of its movement for that call to tick() becomes "velocity * t".

This has the additional benefit of decoupling your physics simulation from the frame-rate.

I see pygame mentioned below: their "pygame.time.Clock.tick()" method is meant to be used this way, as it returns the number of seconds since the last time you called it.

like image 100
mike Avatar answered Nov 14 '22 22:11

mike