Search code examples
pythonmultithreadingcelerybottlebackground-task

Long running background task in a Bottle application


I've been looking for a way to run a continuous background task in a Python Bottle application whilst simultaneously using Gevent for handling requests:

from gevent import monkey; monkey.patch_all()
from time import sleep
from bottle import route, run

# run_background_function()
# ^^^^ starts a single background task that runs every few seconds
# and continues for the life of the whole Bottle application.

@route('/simple-request')
def simple_request():
    # a simple function that returns a rendered page and
    # is capable of serving multiple requests
    return rendered_page()

run(host='0.0.0.0', port=8080, server='gevent')

I've read though many stackoverflow threads and 7 full tutorials thus far including Gevent, threading, celery, rabbitmq, redis and have no idea what I should be using to achieve this ability. Celery, RabbitMQ and Redis all seem insanely difficult and overkill for running this one background task, plus I'd prefer to stay with options in the Python standard library if possible.

The tutorials I've found so far start off very basic then suddenly leap into including 3rd party libraries, sockets, specific web frameworks etc. Is there no way of doing this just on the Python threading module?


Solution

  • you can do this with multiprocessing :

    from multiprocessing import Queue, Process
    
    def processor():
        setproctitle('%s - processor ' % (__file__,))
    
        while True:
            time.sleep(1)
            do_stuff()
    
    my_processor = Process(target=processor)