Skip to content

Instantly share code, notes, and snippets.

@natefoo
Last active August 11, 2017 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natefoo/539540a15f910d472598438d86b691ed to your computer and use it in GitHub Desktop.
Save natefoo/539540a15f910d472598438d86b691ed to your computer and use it in GitHub Desktop.
import json
import time
import threading
import uwsgi
from uwsgidecorators import postfork
postfork_function = None
def log(msg, *args):
print '######## [w:%s m:%s] %s' % (uwsgi.worker_id(), uwsgi.mule_id(), msg % args)
class App(object):
def __init__(self):
log('App instantiated')
self._msg_thread = threading.Thread(name='msg_thread', target=self.handle_msgs)
self._msg_thread.daemon = True
def start(self):
if uwsgi.mule_id() > 0:
del(uwsgi.mule_msg_hook)
log('App.start called')
self._msg_thread.start()
def handle_msgs(self):
log('msg_handler starting up')
while True:
log('waiting for message')
msg = json.loads(uwsgi.mule_get_msg())
log('msg received: %s', msg)
def __call__(self, env, start_response):
msg = {
'path': env['PATH_INFO'],
'args': env['QUERY_STRING'],
}
log('sending msg: %s', msg)
uwsgi.mule_msg(json.dumps(msg))
start_response('200 OK', [('Content-Type', 'text/plain')])
return [env['REQUEST_URI']]
def app_module():
log('application starting')
app = App()
global postfork_function
postfork_function = app.start
return app
@postfork
def do_postfork():
postfork_function()
if __name__ == '__main__':
app = app_module()
log('mule is alive, entering loop')
while True:
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment