Asynchronous processes alongside sanic process


#1

I have a sanic app that processes requests from the web.
There are other tasks that I want to do alongside the main app like DB and sessions maintenance, which must be run at some intervals.

What would be the preferred way to do this?

It is probably a “nube” question, but I will appreciate any suggestions very much.


#2

My approach would be to put your maintenance tasks in their own separate thread from the rest of your program.

import threading, time

...
def db_maintenance():
  while True:
    # do your maintenance tasks here
    time.sleep(60) # sleep for some number of seconds before re-running
...

if __name__ == "__main__":
  ...
  # create maintenance thread(s)
  d = threading.Thread(name='maintenance', target=db_maintenance)
  d.setDaemon(False) # daemon threads not recommended for db operations
  d.start()
  ...
  

#3

I suppose it depends on how heavy your processes will be. Personally, I prefer to have these background tasks running on a different process and use celery running on a separate machine. Maybe this is more than you need.

You could also achieve the same thing in the same process using asyncio queues. In this case, you should use create_server to run sanic


#4

Thanx a lot guys, will try your suggestions.
Have a great weekend!