|
| 1 | +Concurrent Futures API |
| 2 | +====================== |
| 3 | + |
| 4 | +``lithops.concurrent.futures`` is a drop-in for Python's |
| 5 | +`concurrent.futures <https://docs.python.org/3/library/concurrent.futures.html>`_ |
| 6 | +**Executor interface**. Swap the import and the same client keeps working: |
| 7 | + |
| 8 | +.. code:: python |
| 9 | +
|
| 10 | + # from concurrent.futures import ProcessPoolExecutor, as_completed, wait |
| 11 | + from lithops.concurrent.futures import ProcessPoolExecutor, as_completed, wait |
| 12 | +
|
| 13 | + def same_client(executor): |
| 14 | + with executor: |
| 15 | + future = executor.submit(pow, 323, 1235) |
| 16 | + print(future.result()) |
| 17 | + print(list(executor.map(abs, [-1, 2, -3]))) |
| 18 | +
|
| 19 | + same_client(ProcessPoolExecutor()) |
| 20 | +
|
| 21 | +``ThreadPoolExecutor`` is provided under the same name for import compatibility; |
| 22 | +both names run tasks on Lithops workers. |
| 23 | + |
| 24 | +This is separate from the :doc:`Core API <api_futures>` (``lithops.FunctionExecutor``), |
| 25 | +whose ``map()`` returns futures, which has ``call_async()`` instead of ``submit()``, |
| 26 | +and whose ``wait()`` is a method with Lithops-specific ``return_when`` values. |
| 27 | + |
| 28 | + |
| 29 | +Implemented standard-library surface |
| 30 | +------------------------------------ |
| 31 | + |
| 32 | +The module exports the names application code actually uses: |
| 33 | + |
| 34 | +* **Executor** — ``submit()``, eager ``map()`` (results, not futures), |
| 35 | + ``shutdown(wait=True, *, cancel_futures=False)``, context manager |
| 36 | +* **ProcessPoolExecutor** / **ThreadPoolExecutor** — Lithops-backed pools |
| 37 | +* **Future** — subclass of ``concurrent.futures.Future``: |
| 38 | + ``result()``, ``exception()``, ``done()``, ``running()``, ``cancel()``, |
| 39 | + ``cancelled()``, ``add_done_callback()`` |
| 40 | +* **wait** / **as_completed** — same contract and constants |
| 41 | + (``FIRST_COMPLETED``, ``FIRST_EXCEPTION``, ``ALL_COMPLETED``) |
| 42 | +* **Exceptions** — ``CancelledError``, ``TimeoutError``, ``BrokenExecutor``, |
| 43 | + ``InvalidStateError`` |
| 44 | + |
| 45 | +``InterpreterPoolExecutor`` (Python 3.14) is not implemented: Lithops has no |
| 46 | +isolated-interpreter workers. |
| 47 | + |
| 48 | +``map()`` submits one Lithops ``map()`` job for the whole iterable, not one |
| 49 | +``submit()`` per item. Callables travel through a trampoline, so builtins such |
| 50 | +as ``abs`` and ``pow`` work. Its ``chunksize`` is how many items each Lithops |
| 51 | +worker takes, which is what it means for the standard ``ProcessPoolExecutor`` |
| 52 | +too; left unset, the ``chunksize`` of the Lithops configuration applies rather |
| 53 | +than the standard library default of one item per worker. |
| 54 | + |
| 55 | +.. code:: python |
| 56 | +
|
| 57 | + from lithops.concurrent.futures import FunctionExecutor, as_completed |
| 58 | +
|
| 59 | + def load(url): |
| 60 | + return url, len(url) |
| 61 | +
|
| 62 | + with FunctionExecutor() as executor: |
| 63 | + futures = {executor.submit(load, url): url for url in ('a', 'bb', 'ccc')} |
| 64 | + for future in as_completed(futures): |
| 65 | + url, size = future.result() |
| 66 | + print(url, size) |
| 67 | +
|
| 68 | +Mode-specific subclasses ``LocalhostExecutor``, ``ServerlessExecutor`` and |
| 69 | +``StandaloneExecutor`` pin the Lithops execution mode the same way the Core API |
| 70 | +executors do. You can wrap an existing Core API executor:: |
| 71 | + |
| 72 | + import lithops |
| 73 | + from lithops.concurrent.futures import FunctionExecutor |
| 74 | + |
| 75 | + fexec = lithops.FunctionExecutor() |
| 76 | + with FunctionExecutor(executor=fexec) as executor: |
| 77 | + print(executor.submit(pow, 2, 8).result()) |
| 78 | + |
| 79 | + |
| 80 | +Runtime differences |
| 81 | +------------------- |
| 82 | + |
| 83 | +The *API* matches ``concurrent.futures``. The *runtime* is Lithops: |
| 84 | + |
| 85 | +* Workers are Lithops activations, not local threads or ``multiprocessing`` processes. |
| 86 | + ``mp_context``, ``max_tasks_per_child``, and ``thread_name_prefix`` are ignored. |
| 87 | +* ``initializer`` / ``initargs`` are not supported (workers are ephemeral) and raise |
| 88 | + ``NotImplementedError`` if provided. |
| 89 | +* ``cancel()`` cannot stop a job Lithops has already dispatched. ``submit()`` marks |
| 90 | + the future as running immediately, so ``cancel()`` returns ``False``, and |
| 91 | + ``shutdown(cancel_futures=True)`` therefore still waits the calls out. |
| 92 | +* Lithops job options (``runtime_memory``, ``extra_env``, ``execution_timeout``, |
| 93 | + ``include_modules``, ``exclude_modules``) are set on the executor. Keyword |
| 94 | + arguments to ``submit(fn, *args, **kwargs)`` are passed to ``fn``. |
| 95 | +* Each ``Future`` also exposes ``lithops_future`` and ``stats``. |
| 96 | +* A call Lithops loses track of raises ``RuntimeError`` rather than handing |
| 97 | + back a silent ``None``. It fails that one future; the executor stays usable. |
| 98 | +* ``RetryingFunctionExecutor`` cannot be wrapped. Its retries are driven from |
| 99 | + its own ``wait()``, which this adapter never calls, so wrapping it would |
| 100 | + quietly give you no retries at all. Wrap the ``FunctionExecutor`` it holds. |
| 101 | + |
| 102 | +Completion is tracked by the Lithops job monitor, the same one the native |
| 103 | +``wait()`` uses: one batched poll per round for the whole job rather than one |
| 104 | +status read per call. Results are downloaded off the tracking thread, so a |
| 105 | +slow object does not hold up the futures behind it, and ``done()`` never |
| 106 | +blocks on storage. |
| 107 | + |
| 108 | +.. automodule:: lithops.concurrent.futures |
| 109 | + :members: FunctionExecutor, LocalhostExecutor, ServerlessExecutor, |
| 110 | + StandaloneExecutor, ProcessPoolExecutor, ThreadPoolExecutor, |
| 111 | + Future, wait, as_completed |
| 112 | + :show-inheritance: |
0 commit comments