Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update uwsgidecorators #2268

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 48 additions & 64 deletions uwsgidecorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import uwsgi

if uwsgi.masterpid() == 0:
raise Exception(
"you have to enable the uWSGI master process to use this module")
raise Exception("you have to enable the uWSGI master process to use this module")

spooler_functions = {}
mule_functions = {}
Expand All @@ -21,14 +20,14 @@
# Python3 compatibility
def _encode1(val):
if sys.version_info >= (3, 0) and isinstance(val, str):
return val.encode('utf-8')
return val.encode("utf-8")
else:
return val


def _decode1(val):
if sys.version_info >= (3, 0) and isinstance(val, bytes):
return val.decode('utf-8')
return val.decode("utf-8")
else:
return val

Expand All @@ -53,31 +52,31 @@ def manage_spool_request(vars):
# To check whether 'args' is in vals or not - decode the keys first,
# because in python3 all keys in 'vals' are have 'byte' types
vars = dict((_decode1(K), V) for (K, V) in vars.items())
if 'args' in vars:
for k in ('args', 'kwargs'):
if "args" in vars:
for k in ("args", "kwargs"):
vars[k] = pickle.loads(vars.pop(k))

vars = _decode_from_spooler(vars)
f = spooler_functions[vars['ud_spool_func']]
f = spooler_functions[vars["ud_spool_func"]]

if 'args' in vars:
ret = f(*vars['args'], **vars['kwargs'])
if "args" in vars:
ret = f(*vars["args"], **vars["kwargs"])
else:
ret = f(vars)

return int(vars.get('ud_spool_ret', ret))
return int(vars.get("ud_spool_ret", ret))


def postfork_chain_hook():
for f in postfork_chain:
f()


uwsgi.spooler = manage_spool_request
uwsgi.post_fork_hook = postfork_chain_hook


class postfork(object):

def __init__(self, f):
if callable(f):
self.wid = 0
Expand All @@ -96,7 +95,6 @@ def __call__(self, *args, **kwargs):


class _spoolraw(object):

def __call__(self, *args, **kwargs):
arguments = self.base_dict.copy()
if not self.pass_arguments:
Expand All @@ -106,41 +104,42 @@ def __call__(self, *args, **kwargs):
arguments.update(kwargs)
else:
spooler_args = {}
for key in ('message_dict', 'spooler', 'priority', 'at', 'body'):
for key in ("message_dict", "spooler", "priority", "at", "body"):
if key in kwargs:
spooler_args.update({key: kwargs.pop(key)})
arguments.update(spooler_args)
arguments.update(
{'args': pickle.dumps(args), 'kwargs': pickle.dumps(kwargs)})
{"args": pickle.dumps(args), "kwargs": pickle.dumps(kwargs)}
)
return uwsgi.spool(_encode_to_spooler(arguments))

# For backward compatibility (uWSGI < 1.9.13)
def spool(self, *args, **kwargs):
return self.__class__.__call__(self, *args, **kwargs)

def __init__(self, f, pass_arguments):
if 'spooler' not in uwsgi.opt:
if "spooler" not in uwsgi.opt:
raise Exception(
"you have to enable the uWSGI spooler to use @%s decorator" % self.__class__.__name__)
"you have to enable the uWSGI spooler to use @%s decorator"
% self.__class__.__name__
)
self.f = f
spooler_functions[self.f.__name__] = self.f
# For backward compatibility (uWSGI < 1.9.13)
self.f.spool = self.__call__
self.pass_arguments = pass_arguments
self.base_dict = {'ud_spool_func': self.f.__name__}
self.base_dict = {"ud_spool_func": self.f.__name__}


class _spool(_spoolraw):

def __call__(self, *args, **kwargs):
self.base_dict['ud_spool_ret'] = str(uwsgi.SPOOL_OK)
self.base_dict["ud_spool_ret"] = str(uwsgi.SPOOL_OK)
return _spoolraw.__call__(self, *args, **kwargs)


class _spoolforever(_spoolraw):

def __call__(self, *args, **kwargs):
self.base_dict['ud_spool_ret'] = str(uwsgi.SPOOL_RETRY)
self.base_dict["ud_spool_ret"] = str(uwsgi.SPOOL_RETRY)
return _spoolraw.__call__(self, *args, **kwargs)


Expand All @@ -163,7 +162,6 @@ def spoolforever(f=None, pass_arguments=False):


class mulefunc(object):

def __init__(self, f):
if callable(f):
self.fname = f.__name__
Expand All @@ -174,14 +172,17 @@ def __init__(self, f):
self.fname = None

def real_call(self, *args, **kwargs):
uwsgi.mule_msg(pickle.dumps(
{
'service': 'uwsgi_mulefunc',
'func': self.fname,
'args': args,
'kwargs': kwargs
}
), self.mule)
uwsgi.mule_msg(
pickle.dumps(
{
"service": "uwsgi_mulefunc",
"func": self.fname,
"args": args,
"kwargs": kwargs,
}
),
self.mule,
)

def __call__(self, *args, **kwargs):
if not self.fname:
Expand All @@ -198,14 +199,14 @@ def mule_msg_dispatcher(message):
except pickle.UnpicklingError:
return

if msg['service'] == 'uwsgi_mulefunc':
return mule_functions[msg['func']](*msg['args'], **msg['kwargs'])
if msg["service"] == "uwsgi_mulefunc":
return mule_functions[msg["func"]](*msg["args"], **msg["kwargs"])


uwsgi.install_mule_msg_hook(mule_msg_dispatcher)


class rpc(object):

def __init__(self, name):
self.name = name

Expand All @@ -215,7 +216,6 @@ def __call__(self, f):


class farm_loop(object):

def __init__(self, f, farm):
self.f = f
self.farm = farm
Expand All @@ -232,7 +232,6 @@ def __call__(self):


class farm(object):

def __init__(self, name=None, **kwargs):
self.name = name

Expand All @@ -241,7 +240,6 @@ def __call__(self, f):


class mule_brain(object):

def __init__(self, f, num):
self.f = f
self.num = num
Expand All @@ -257,7 +255,6 @@ def __call__(self):


class mule_brainloop(mule_brain):

def __call__(self):
if uwsgi.mule_id() == self.num:
while True:
Expand All @@ -270,7 +267,6 @@ def __call__(self):


class mule(object):

def __init__(self, num):
self.num = num

Expand All @@ -279,13 +275,11 @@ def __call__(self, f):


class muleloop(mule):

def __call__(self, f):
postfork_chain.append(mule_brainloop(f, self.num))


class mulemsg_loop(object):

def __init__(self, f, num):
self.f = f
self.num = num
Expand All @@ -299,7 +293,6 @@ def __call__(self):


class mulemsg(object):

def __init__(self, num):
self.num = num

Expand All @@ -308,22 +301,20 @@ def __call__(self, f):


class signal(object):

def __init__(self, num, **kwargs):
self.num = num
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
return f


class timer(object):

def __init__(self, secs, **kwargs):
self.num = kwargs.get('signum', get_free_signal())
self.num = kwargs.get("signum", get_free_signal())
self.secs = secs
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
Expand All @@ -332,11 +323,10 @@ def __call__(self, f):


class mstimer(object):

def __init__(self, msecs, **kwargs):
self.num = kwargs.get('signum', get_free_signal())
self.num = kwargs.get("signum", get_free_signal())
self.msecs = msecs
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
Expand All @@ -345,29 +335,28 @@ def __call__(self, f):


class cron(object):

def __init__(self, minute, hour, day, month, dayweek, **kwargs):
self.num = kwargs.get('signum', get_free_signal())
self.num = kwargs.get("signum", get_free_signal())
self.minute = minute
self.hour = hour
self.day = day
self.month = month
self.dayweek = dayweek
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
uwsgi.add_cron(self.num, self.minute, self.hour,
self.day, self.month, self.dayweek)
uwsgi.add_cron(
self.num, self.minute, self.hour, self.day, self.month, self.dayweek
)
return f


class rbtimer(object):

def __init__(self, secs, **kwargs):
self.num = kwargs.get('signum', get_free_signal())
self.num = kwargs.get("signum", get_free_signal())
self.secs = secs
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
Expand All @@ -376,11 +365,10 @@ def __call__(self, f):


class filemon(object):

def __init__(self, fsobj, **kwargs):
self.num = kwargs.get('signum', get_free_signal())
self.num = kwargs.get("signum", get_free_signal())
self.fsobj = fsobj
self.target = kwargs.get('target', '')
self.target = kwargs.get("target", b"")

def __call__(self, f):
uwsgi.register_signal(self.num, self.target, f)
Expand All @@ -389,7 +377,6 @@ def __call__(self, f):


class erlang(object):

def __init__(self, name):
self.name = name

Expand All @@ -399,7 +386,6 @@ def __call__(self, f):


class lock(object):

def __init__(self, f):
self.f = f

Expand All @@ -415,7 +401,6 @@ def __call__(self, *args, **kwargs):


class thread(object):

def __init__(self, f):
self.f = f

Expand All @@ -427,7 +412,6 @@ def __call__(self, *args):


class harakiri(object):

def __init__(self, seconds):
self.s = seconds

Expand Down