/
create.py
71 lines (63 loc) · 2.13 KB
/
create.py
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
"""
(*)~---------------------------------------------------------------------------
Pupil - eye tracking platform
Copyright (C) 2012-2019 Pupil Labs
Distributed under the terms of the GNU
Lesser General Public License (LGPL v3.0).
See COPYING and COPYING.LESSER for license details.
---------------------------------------------------------------------------~(*)
"""
import inspect
from tasklib.background.task import BackgroundGeneratorFunction
from tasklib.background.task import BackgroundRoutine
from tasklib.background.patches import IPCLoggingPatch, KeyboardInterruptHandlerPatch
def create(
name,
routine_or_generator_function,
pass_shared_memory=False,
args=None,
kwargs=None,
patches=None,
):
"""
Creates the right background task for your type of task.
Normally, you would not use this directly, but use a PluginTaskManager and the
create_background_task() method there.
If you don't use a manager, you need to regularly call update() on the returned task
to trigger callbacks etc.
See the docstring for PluginTaskManager.create_background_task()
(in tasklib.manager.py) for information about the different parameters!
"""
if args is None:
args = ()
if kwargs is None:
kwargs = {}
if patches is None:
patches = [
IPCLoggingPatch(),
KeyboardInterruptHandlerPatch(),
]
if inspect.isgeneratorfunction(routine_or_generator_function):
return BackgroundGeneratorFunction(
name,
routine_or_generator_function,
pass_shared_memory,
args,
kwargs,
patches,
)
elif inspect.isroutine(routine_or_generator_function):
return BackgroundRoutine(
name,
routine_or_generator_function,
pass_shared_memory,
args,
kwargs,
patches,
)
else:
raise TypeError(
"Cannot create background task from {}. It must be a "
"routine (function, method, lambda) or generator "
"function!".format(routine_or_generator_function)
)