How to run a background job every 2 seconds in Frappe?

I’m trying to run a background function in Frappe at an interval of every 2 seconds.

I know that the built-in Frappe scheduler supports only minutely, hourly, daily, etc., through hooks.py. But in my use case, I need a function that runs more frequently (every 2 seconds).

I have tried using a while True loop with time.sleep(2), but the issue is that:

  • There’s no clean way to stop the loop once it starts.
  • If I make code changes, the old loop keeps running and doesn’t pick up the new code without restarting manually.

Hi, Frappe scheduler can’t run every 2 sec (min granularity is 1 min).

  • Avoid while True + sleep, as it causes uncontrolled loops.
  • Use background worker + cron/external scheduler.