Coverage for src/python/ensembl/vep/workflow/worker/base.py: 100%
8 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""The worker contract: one payload in, one serialisable result out."""
16from __future__ import annotations
18from typing import Protocol, TypeVar, runtime_checkable
20__all__ = ["Worker"]
22PayloadT_contra = TypeVar("PayloadT_contra", contravariant=True)
23ResultT_co = TypeVar("ResultT_co", covariant=True)
26@runtime_checkable
27class Worker(Protocol[PayloadT_contra, ResultT_co]):
28 """Process-local runtime that runs one payload at a time.
30 A worker is built once per process, after that process starts, and then reused: it holds open
31 sources and warm caches that neither cross a process boundary nor survive a rebuild. So what
32 the executor transports is the factory, never the worker, and the worker itself never has to
33 be serialisable -- only its payloads and its results do.
35 One method, because that is all an executor needs of it. What a payload holds, and what the
36 result carries, belongs to the kind of work: ``AnnotationBufferWorker`` takes a payload of
37 input records and answers with detached annotations.
38 """
40 def run(self, payload: PayloadT_contra) -> ResultT_co:
41 """Return the result of this payload, against the sources this worker holds.
43 Args:
44 payload (PayloadT_contra): One unit of work, as its executor's payloads are grouped.
46 Returns:
47 ResultT_co: What the payload produced, serialisable enough to cross back.
48 """