Skip to content

Instantly share code, notes, and snippets.

@smason
Created April 25, 2018 17:58
Show Gist options
  • Save smason/519566441dd7eefb0659e2a1a01cf6af to your computer and use it in GitHub Desktop.
Save smason/519566441dd7eefb0659e2a1a01cf6af to your computer and use it in GitHub Desktop.
Using uWSGI Mules and Farms to prioritise background work
[uwsgi]
http = :8000
wsgi-file = mules.py
need-app = true
master = true
enable-threads = true
processes = 2
mules = 5
farm = limited:1
harakiri = 120
buffer-size = 32768
import time
import uwsgidecorators
# execute mule jobs from the "limited" farm
@uwsgidecorators.mulefunc('limited')
def foo(ts):
print("limited sleeping {}".format(ts))
time.sleep(2)
print("limited done {}".format(ts))
# execute mule jobs from any farm
@uwsgidecorators.mulefunc
def mule(ts):
print("any sleeping {}".format(ts))
time.sleep(2)
print("any done {}".format(ts))
started = time.time()
def application(env, start_response):
ts = time.time() - started
path = env['PATH_INFO']
if path == '/foo':
foo(ts)
elif path == '/mule':
mule(ts)
start_response('200 OK', [
('Content-Type','text/html'),
])
return [b"Hello World"]
#!/bin/sh
# start uwsgi by running:
# uwsgi --ini mules.ini
# then run this script
# perform some get requests
for i in $(seq 10); do
curl http://localhost:8000/limited http://localhost:8000/mule
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment