Decorators in uWSGI

Posted by Jonas Sandström on Fri 01 January 2021

Last year I started to learn more about the uWSGI webserver for Python. This was when I realised that The uWSGI project has decorators which can be used in interesting ways. I thought It was neat because I mainly use uWSGI as a wsgi server for serving Flask websites and APIs. Since I found out about this I can now use it for more. Especially since I already have it among the dependencies for a lot of my projects. Here are some exampels of uWSGI decorators:

@filemon

@filemon("my-notes/notes.txt")
def file_has_been_modified(filename):
    print("notes.txt has been modified")

The @filemon decorator could be used as the bases for a simple monitoring tool, mabye triggering a backup, copying and minify an updated file or just printing a log message like the example above.

@cron

# positional arguments: minute, hour, day, month, weekday (-1 means no value)
@cron(30, 5, -1, -1, -1)
def run_that_daily_script():
    calculate_daily_cost()

If you know about unix cron jobs this is basically it, but as a decorator on a python function. This example triggers the calculate_daily_cost 5:30 each day

@signal

@signal(2)
def my_signal(num):
    print("The process was interrupted)

This decorator gets triggerd by Unix signals. This work similar to the signal module in the Python standard library.