Skip to content

Instantly share code, notes, and snippets.

@jpic
Last active August 23, 2018 00:24
Show Gist options
  • Save jpic/d28333b0573c3c555fbe6e55862ecddb to your computer and use it in GitHub Desktop.
Save jpic/d28333b0573c3c555fbe6e55862ecddb to your computer and use it in GitHub Desktop.
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