Sanic: add scheduler feature

Created on 4 Aug 2017  ·  2Comments  ·  Source: sanic-org/sanic

Proposal

Sanic could run configured schedule tasks. Users can define schedule as delayed or interval or cron-like pattern.

This is helpful when app need to update data, gather statistics, release unneeded resources automatically.

Approach

Using functions like asyncio.sleep(), an coroutine with an endless loop could check schedule periodically, and trigger functions that on schedule.

Multiple workers

Multiple workers can be problem, when all worker trying to be scheduler at same time.

One solution is, giving every worker an id, and always using worker with id 0 to be the scheduler. This seems will work with sanic's builtin server, but as far as I know, gunicorn does not support worker with id.

Another solution is, to use some shared queues, like redis, to publish scheduled jobs. That seems fine, but it requires third party.

Any thoughts?

Most helpful comment

It doesn't have to be a builtin feature of Sanic, it'd be better to create a lib.

All 2 comments

It doesn't have to be a builtin feature of Sanic, it'd be better to create a lib.

I think this would be more trouble than it's worth.

If you have your heart set out with running this in python you could use @dbader's library https://github.com/dbader/schedule and run it as a separate service that talks to your sanic application through maybe a private API?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

misakar picture misakar  ·  4Comments

sirex picture sirex  ·  4Comments

1067511899 picture 1067511899  ·  3Comments

eseglem picture eseglem  ·  4Comments

woutor picture woutor  ·  3Comments