Skip to content

Instantly share code, notes, and snippets.

@jpic
Last active Aug 23, 2018
Embed
What would you like to do?
generic uwsgi spooler minimal wrapper poc, base of django-uwsgi-spooler (halted) and django-call (ongoing)
from importlib import import_module
import pickle
from uuid import uuid4
try:
from uwsgi import SPOOL_OK
except ImportError:
SPOOL_OK = True
try:
import uwsgi
except ImportError:
uwsgi = None
def import_string(dotted_path):
"""
Import a dotted module path and return the attribute/class designated by the
last name in the path. Raise ImportError if the import failed.
"""
try:
module_path, class_name = dotted_path.rsplit('.', 1)
except ValueError as err:
raise ImportError("%s doesn't look like a module path" % dotted_path) from err
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError as err:
raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
module_path, class_name)
)
def emit(callback_name, **env):
new_env = {
b'uuid': uuid4(),
b'callback': callback_name,
b'body': pickle.dumps(env)
}
spool = getattr(uwsgi, 'spooler', spooler)
spool(new_env)
def spooler(env):
callback = import_string(env[b'callback'])
cb_env = pickle.loads(env[b'body'])
cb_env['uuid'] = env[b'uuid']
callback(**cb_env)
return SPOOL_OK
if uwsgi:
uwsgi.spooler = spooler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment