Represents a local deployed HyperQueue infrastructure. You can use `LocalCluster` to quickly spin up a HyperQueue server along with a set of workers locally. The cluster can be used as a context manager. It will be stopped when the context ends: ```python with LocalCluster
| 18 | |
| 19 | |
| 20 | class LocalCluster: |
| 21 | """ |
| 22 | Represents a local deployed HyperQueue infrastructure. |
| 23 | |
| 24 | You can use `LocalCluster` to quickly spin up a HyperQueue server along with a set of workers |
| 25 | locally. |
| 26 | |
| 27 | The cluster can be used as a context manager. It will be stopped when the context ends: |
| 28 | ```python |
| 29 | with LocalCluster() as cluster: |
| 30 | client = cluster.client() |
| 31 | ... |
| 32 | # The cluster was stopped |
| 33 | ``` |
| 34 | """ |
| 35 | |
| 36 | def __init__( |
| 37 | self, |
| 38 | server_dir: Optional[Path] = None, |
| 39 | worker_config: Optional[WorkerConfig] = None, |
| 40 | ): |
| 41 | """ |
| 42 | :param server_dir: Server directory where will the cluster store its files. |
| 43 | :param worker_config: Configuration of workers spawned in the cluster. |
| 44 | """ |
| 45 | self.cluster = Cluster(server_dir) |
| 46 | if worker_config is not None: |
| 47 | self.start_worker(worker_config) |
| 48 | |
| 49 | def start_worker(self, config: WorkerConfig = None): |
| 50 | """ |
| 51 | Adds a new worker with the given `config` to the cluster. |
| 52 | """ |
| 53 | config = config if config is not None else WorkerConfig() |
| 54 | cores = config.cores or multiprocessing.cpu_count() |
| 55 | self.cluster.add_worker(cores) |
| 56 | |
| 57 | def client(self, **client_args) -> Client: |
| 58 | """ |
| 59 | Creates a client connected to this cluster. |
| 60 | """ |
| 61 | return Client(self.cluster.server_dir, **client_args) |
| 62 | |
| 63 | def stop(self): |
| 64 | """ |
| 65 | Stops the server and all workers of this cluster. |
| 66 | """ |
| 67 | self.cluster.stop() |
| 68 | |
| 69 | def __enter__(self): |
| 70 | return self |
| 71 | |
| 72 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 73 | self.stop() |
no outgoing calls