Skip to content

Instantly share code, notes, and snippets.

@svartalf
Last active January 4, 2016 18:39
Show Gist options
  • Save svartalf/8661828 to your computer and use it in GitHub Desktop.
Save svartalf/8661828 to your computer and use it in GitHub Desktop.
Trying to make a uwsgi + fastrouter + workers warming up. NOT WORKING NOW!
[uwsgi]
master = true
socket = 127.0.0.1:8080
pidfile = /tmp/svartalf.pid
processes = 4
home = /home/svartalf/projects/svartalf/.env
pythonpath = /home/svartalf/projects/svartalf/
mount = /=/home/svartalf/projects/irk/wsgi.py
enable-threads = true
optimize = 1
single-interpreter = true
unsubscribe-on-graceful-reload = true
plugins = python2
#!/usr/bin/python
import os
import sys
import warnings
import socket
import struct
import uwsgi
import uwsgidecorators
current = os.path.dirname(os.path.abspath(__file__))
sys.path.extend([current, os.path.join(current, 'apps')])
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
warnings.filterwarnings("ignore", category=DeprecationWarning)
import django.core.handlers.wsgi
from django.conf import settings
application = django.core.handlers.wsgi.WSGIHandler()
warm_signal = uwsgidecorators.get_free_signal()
@uwsgidecorators.postfork
def warm_up():
application({
'REQUEST_METHOD': 'GET',
'SERVER_NAME': settings.BASE_HOST,
'SERVER_PORT': 80,
'PATH': '/',
'wsgi.input': sys.stdin,
}, lambda x, y: None)
uwsgi.log('Warming up worker #{0}'.format(uwsgi.worker_id()))
uwsgi.queue_set(uwsgi.worker_id(), str(1))
uwsgi.signal(warm_signal)
def subscribe():
"""Sending subscribe packet to uwsgi fastrouter"""
payload = {
'key': 'svartalf.local', # My local domain
'address': '127.0.0.1:8080'
}
message = ''
for k, v in payload.iteritems():
message += struct.pack('<h', len(k)) + k
message += struct.pack('<h', len(v)) + v
message = struct.pack('<BhB', 224, len(message), 0) + message
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(message, ('127.0.0.1', 7000)) # fastrouter local address
@uwsgidecorators.signal(warm_signal, target='worker')
def on_warm_signal(signum):
"""Stupid way to check if all workers are warmed up now"""
for i in range(1, uwsgi.numproc + 1):
worker_warmed = uwsgi.queue_get(i) == '1'
if not worker_warmed:
return
subscribe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment