3 """A decorator to help with dead simple parallelization."""
24 _funct: typing.Optional[typing.Callable] = None,
26 method: Method = Method.THREAD
30 @parallelize # defaults to thread-mode
31 def my_function(a, b, c) -> int:
32 ...do some slow / expensive work, e.g., an http request
34 @parallelize(method=Method.PROCESS)
35 def my_other_function(d, e, f) -> str:
36 ...do more really expensice work, e.g., a network read
38 @parallelize(method=Method.REMOTE)
39 def my_other_other_function(g, h) -> int:
40 ...this work will be distributed to a remote machine pool
42 This decorator will invoke the wrapped function on:
44 Method.THREAD (default): a background thread
45 Method.PROCESS: a background process
46 Method.REMOTE: a process on a remote host
48 The wrapped function returns immediately with a value that is
49 wrapped in a SmartFuture. This value will block if it is either
50 read directly (via a call to result._resolve) or indirectly (by
51 using the result in an expression, printing it, hashing it,
52 passing it a function argument, etc...). See comments on the
53 SmartFuture class for details.
55 Note: you may stack @parallelized methods and it will "work".
56 That said, having multiple layers of Method.PROCESS or
57 Method.REMOTE may prove to be problematic because each process in
58 the stack will use its own independent pool which may overload
59 your machine with processes or your network with remote processes
60 beyond the control mechanisms built into one instance of the pool.
63 def wrapper(funct: typing.Callable):
65 @functools.wraps(funct)
66 def inner_wrapper(*args, **kwargs):
67 # Look for as of yet unresolved arguments in _funct's
68 # argument list and resolve them now.
71 newargs.append(smart_future.SmartFuture.resolve(arg))
74 newkwargs[kw] = smart_future.SmartFuture.resolve(
79 if method == Method.PROCESS:
80 executor = executors.DefaultExecutors().process_pool()
81 elif method == Method.THREAD:
82 executor = executors.DefaultExecutors().thread_pool()
83 elif method == Method.REMOTE:
84 executor = executors.DefaultExecutors().remote_pool()
85 assert executor is not None
87 future = executor.submit(funct, *newargs, **newkwargs)
89 # Wrap the future that's returned in a SmartFuture object
90 # so that callers do not need to call .result(), they can
91 # just use is as normal.
92 return smart_future.SmartFuture(future)
99 return wrapper(_funct)