Last active
August 11, 2017 14:28
-
-
Save natefoo/539540a15f910d472598438d86b691ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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