Backgroundservice from .net alternative


At work, we often use c# .net core. I’m looking to see if we theoretically port our code to python django.
In netcore, we often use BackgroundServices to connect to messagebrokers. This is usually a very simple process of just adding the hosted service in the startup code.

Most of the django libraries i’ve seen use management commands to execute workers, which is a problem for us, because we run containerised applications, without the ability to use something like a Procfile (so we can’t really execute the command for gunicorn and the workers).

Now you might be thinking, why don’t you just spin up a second container for the worker? Which brings up the next problem: health checks. We use http healthchecks, so in every worker project we also include an api, which checks dependencies etc. (and honestly this might be the thing that needs changing…)

Did anyone do this before?

This is what is kind of working for me now:

def run_huey():

threading.Thread(target=run_huey, daemon=True).start()

This will only throw an error at the start of the script, because the huey is using signal handlers, and the process runs in a seperate thread.

I’ve tries subprocess as well, but was unable to combine the output of django/worker

“Everyone” runs separate containers. Trying to run the two disparate workloads (web, background) in the same process is hard to debug and hard to keep performant since they normally need quite different resources.

You can make a simple HTTP healthcheck thread inside huey if that’s what you want.

Hi Adam,

I don’t know gunicorn but you can use uwsgi and enable the thread you can use a thread for your background tasks.