Skip to content

Instantly share code, notes, and snippets.

@ipmb
Created May 13, 2018 00:48
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 ipmb/1a0485d0e4a190d5ba183e652155829c to your computer and use it in GitHub Desktop.
Save ipmb/1a0485d0e4a190d5ba183e652155829c to your computer and use it in GitHub Desktop.
uwsgi as lib
# UWSGI_AS_LIB=`pwd`/saltdash/libuwsgi.so pip install uwgi
# via https://gist.github.com/sixninetynine/c5c2c0047ea510d9db6c4f99f09c341e
import sys
import os
import ctypes
try:
from importlib import resources
except ImportError:
import importlib_resources as resources
def uwsgi_run(uwsgi_binary, uwsgi_args):
# load the uwsgi library in the global namespace
with resources.path('saltdash', 'libuwsgi.so') as libuwsgi:
uwsgi = ctypes.CDLL(str(libuwsgi), mode=ctypes.RTLD_GLOBAL)
args = [
uwsgi_binary,
'--binary-path', uwsgi_binary,
'--module', 'saltdash.wsgi',
]
uwsgi_args = uwsgi_args or [
'--master',
'--workers', str(os.cpu_count() or 2),
'--http', '127.0.0.1:8000',
'--thunder-lock',
'--enable-threads',
'--strict',
'--need-app',
'--auto-procname',
'--procname-prefix-spaced', 'saltdash:',
]
args.extend(uwsgi_args)
# build command line args
argv = (ctypes.c_char_p * (len(args) + 1))()
for pos, arg in enumerate(args):
argv[pos] = bytes(arg, 'utf-8')
# inform the uwsgi engine, the passed environ is not safe to overwrite
envs = (ctypes.c_char_p * 1)()
# enter into uWSGI !!!
uwsgi.uwsgi_init(len(args), argv, envs)
if __name__ == '__main__':
uwsgi_run(sys.argv[0], sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment