python periodic timer

91678ba6f2a868d85baeeb60725f7cfe
0
Arg0 101 May 12, 2008 at 12:24

I’m trying to make a periodic timer and i’ve ran into a problem, i get thhis error,

“signal.signal(signal.SIGALRM, alarm_handler)
AttributeError: ‘module’ object has no attribute ‘SIGALRM’”

anyone know how whats wrong?

import signal, time
interval = 1.0
ticks = 0
def alarm_handler(signo,frame):
    global ticks
    print "Alarm ", ticks
    ticks = ticks + 1
    signal.alarm(interval)                # Schedule a new alarm

signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(interval)
# Spin forever--should see handler being called every second
while 1:
    pass

10 Replies

Please log in or register to post a reply.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 May 13, 2008 at 17:08

Windows doesn’t support SIGALRM. Try it on a real POSIX system and it will work. I tested it on Linux and it’s fine.

91678ba6f2a868d85baeeb60725f7cfe
0
Arg0 101 May 13, 2008 at 18:53

@monjardin

Windows doesn’t support SIGALRM. Try it on a real POSIX system and it will work. I tested it on Linux and it’s fine.

well what do i use for windows?

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 May 13, 2008 at 20:44

That depends on what you’re doing with it, but you could get the same functionality with the first result of a google search for “python timer”: http://docs.python.org/lib/timer-objects.html

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 May 13, 2008 at 22:04

monjardin: I see that JwN meter going up ;)

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 May 14, 2008 at 08:52

Did it ever go down?

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 May 15, 2008 at 16:53

It’s been in the red for a few years. So, I guess it’s about time I update it to a level more reflective of current my “jadedness”.

91678ba6f2a868d85baeeb60725f7cfe
0
Arg0 101 May 17, 2008 at 02:54

Ok back on topic, i need a delay timer or something like that so i can set the turn rate of the players sprite. right now i’m using pygame.time.wait(20), this slows down the game and ya that sucks. So how can i have it so it turns at a given (and can be changed) interval?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 May 17, 2008 at 03:24

Well, the way most games do that is they actually measure how long it takes to do one frame (rendering, logic, everything), and scale all rates of change by that value. For instance, you’d express your turn rate in degrees per second, and each frame you would calculate the amount to turn by multiplying that by the frame time in seconds.

To measure the frame time, you can just stick something in your main game loop that calls pygame.time.get_ticks each frame and calculates the difference from the previous frame’s ticks value. This is the number of milliseconds the last frame took, which is normally a pretty good estimate of the number of milliseconds the current frame will take.

91678ba6f2a868d85baeeb60725f7cfe
0
Arg0 101 May 17, 2008 at 23:33

ok, i have no idea how to even start to code that. can you show some code?

91678ba6f2a868d85baeeb60725f7cfe
0
Arg0 101 May 21, 2008 at 19:56

@ccoff

i[](http://www.funingame.com)like [](http://www.funingame.com/eve.html)your[](http://www.funingame.com/guildwars.html)article[](http://www.funingame.com/maplestory.html)very[](http://www.allgamegold.com)much。

Huh?