forked from seung-lab/python-task-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueablefns.py
More file actions
129 lines (110 loc) · 3.42 KB
/
queueablefns.py
File metadata and controls
129 lines (110 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from collections import namedtuple
import functools
import inspect
import orjson
import copy
import re
from collections import OrderedDict
from functools import partial
import numpy as np
REGISTRY = {}
FunctionTaskLite = namedtuple("FunctionTaskLite", [ "key", "args", "kwargs", "id" ])
class UnregisteredFunctionError(BaseException):
pass
def totask(data, ident=-1):
if isinstance(data, FunctionTask):
return data
if isinstance(data, partial) or callable(data):
return func2task(data, ident)
if type(data) is bytes:
data = data.decode('utf8')
if isinstance(data, str):
data = orjson.loads(data)
data[3] = ident
return FunctionTask(*data)
def func2task(fn, ident):
args = []
kwargs = {}
rawfn = fn
while isinstance(rawfn, partial):
args += rawfn.args
kwargs.update(rawfn.keywords)
rawfn = rawfn.func
if not argsokay(rawfn, args, kwargs):
raise TypeError("{} didn't get valid arguments. Got: {}, {}. Expected: {}".format(
rawfn, args, kwargs, inspect.getfullargspec(rawfn)
))
return FunctionTask(
(rawfn.__module__, rawfn.__name__),
args,
kwargs,
ident
)
def tofunc(task):
if callable(task):
return task
fn = REGISTRY.get(tuple(task[0]), None)
if fn is None:
raise UnregisteredFunctionError("{} is not registered as a queuable function.".format(task.key))
return partial(fn, *task[1], **task[2])
class FunctionTask():
__slots__ = ['key', 'args', 'kwargs', 'id', '_order']
def __init__(self, key, args, kwargs, id=None):
self.key = tuple(key)
self.args = args
self.kwargs = kwargs
self.id = id
self._order = ('key', 'args', 'kwargs', 'id')
def __getitem__(self, idx):
return getattr(self, self._order[idx])
def __setitem__(self, idx, value):
setattr(self, self._order[idx], value)
def __iter__(self):
raise TypeError("FunctionTask is not an iterable.")
def payload(self):
return FunctionTaskLite(self.key, self.args, self.kwargs, self.id)
def execute(self, *args, **kwargs):
self(*args, **kwargs)
def tofunc(self):
fn = REGISTRY.get(tuple(self.key), None)
if fn is None:
raise UnregisteredFunctionError("{} is not registered as a queuable function.".format(self.key))
return partial(fn, *self.args, **self.kwargs)
def __repr__(self):
return "FunctionTask({},{},{},\"{}\")".format(self.key, self.args, self.kwargs, self.id)
def __call__(self):
return self.tofunc()()
def jsonifyable(obj):
if hasattr(obj, 'serialize') and callable(obj.serialize):
return obj.serialize()
try:
iter(obj)
except TypeError:
return obj
if isinstance(obj, bytes):
return obj.decode('utf8')
elif isinstance(obj, str):
return obj
if isinstance(obj, list) or isinstance(obj, tuple):
return [ jsonifyable(x) for x in obj ]
for key, val in obj.items():
if isinstance(val, np.ndarray):
obj[key] = val.tolist()
elif isinstance(val, dict):
obj[key] = jsonifyable(val)
elif isinstance(val, list):
obj[key] = [ jsonifyable(x) for x in val ]
elif hasattr(val, 'serialize') and callable(val.serialize):
obj[key] = val.serialize()
return obj
def argsokay(fn, args, kwargs):
sig = inspect.signature(fn)
try:
sig.bind(*args, **kwargs)
except TypeError:
return False
return True
def queueable(fn):
"""Register the input function as queueable and executable via TaskQueue."""
REGISTRY[(fn.__module__, fn.__name__)] = fn
return fn