1 |
1865
|
aaronmk
|
# Runnable object
|
2 |
|
|
|
3 |
1867
|
aaronmk
|
import copy
|
4 |
|
|
|
5 |
|
|
import eval_
|
6 |
|
|
|
7 |
1865
|
aaronmk
|
class Runnable:
|
8 |
1867
|
aaronmk
|
'''A picklable function call'''
|
9 |
|
|
|
10 |
1865
|
aaronmk
|
def __init__(self, func, *args, **kw_args):
|
11 |
|
|
self.func = func
|
12 |
|
|
self.args = args
|
13 |
|
|
self.kw_args = kw_args
|
14 |
|
|
|
15 |
|
|
def __call__(self): return self.func(*self.args, **self.kw_args)
|
16 |
1867
|
aaronmk
|
|
17 |
|
|
def __getstate__(self):
|
18 |
|
|
state = copy.copy(self.__dict__) # shallow copy
|
19 |
|
|
state['func'] = eval_.func2name(state['func']) # make func picklable
|
20 |
|
|
return state
|
21 |
|
|
|
22 |
|
|
def __setstate__(self, state):
|
23 |
|
|
state['func'] = eval_.name2func(*state['func'])
|
24 |
|
|
self.__dict__ = state
|