Itai Elidan
Itai Elidan

Reputation: 272

Python timers with no threading

I have a Python flask-socketio server, and I want to sync a timer to the clients. I tried doing the timer in the client, but it doesn't sync reliably, so I decided I need to do the timer on the server. The problem is Threading requires a lot of expensive processing from the server, and I don't want to deal with eventlet, gevent, or the standard threading. Is there a way to sync a timer reliably without threading?

Upvotes: 0

Views: 162

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

I'm not sure what you mean by "Threading requires a lot of expensive processing", but in any case, threading is incompatible with eventlet and with gevent, so it wouldn't work anyway.

Gevent and eventlet both provide a replacement module that implements everything in threading using greenlets. For example, you can import the Timer class from eventlet with:

from eventlet.green.threading import Timer

As far as accuracy I'm not sure what your expectations are. Since greenlets are cooperative, exact timers are not possible, so in general the timeouts that you specify are a minimum, so the timer may fire a bit later than the requested time depending on how other tasks cooperate with the async loop.

Upvotes: 1

Related Questions