multiprocessing_connection_manager
drunc.grpc_testing_tools.multiprocessing_connection_manager
Multiprocessing Connection Manager
Manages gRPC server processes using Python's multiprocessing module for local execution. Handles process creation, event coordination, and lifecycle management for servers running as separate processes on the same machine.
Classes
MultiprocessingConnectionManager(env_vars=None)
Bases: ProcessConnectionManager
Process connection manager using Python's multiprocessing module.
Executes processes locally using multiprocessing.Process, suitable for single-machine testing scenarios where all gRPC servers run on localhost. Creates and manages ready/stop events for process coordination.
Initialise multiprocessing connection manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_vars
|
Dict[str, str]
|
Environment variables to set for child processes |
None
|
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
Functions
cleanup()
Stop all managed processes and cleanup resources.
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
create_process(process_id, target_func, *args, **kwargs)
Create a multiprocessing.Process handle with coordination events.
Creates ready and stop events for process lifecycle coordination, wraps the target function to set environment variables, and prepares the process for execution. Events are appended to the argument list so the server functions can use them for signalling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_id
|
str
|
Unique identifier for the process |
required |
target_func
|
Any
|
Function to execute in the new process |
required |
*args
|
Arguments to pass to target function |
()
|
|
**kwargs
|
Keyword arguments to pass to target function |
{}
|
Returns:
| Type | Description |
|---|---|
RunningGrpcServer
|
RunningGrpcServer containing the multiprocessing.Process and events |
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
is_process_alive(handle)
Check if a multiprocessing.Process is alive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
RunningGrpcServer
|
RunningGrpcServer to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if process is running, False otherwise |
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
start_process(handle)
Start a multiprocessing.Process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
RunningGrpcServer
|
RunningGrpcServer containing multiprocessing.Process |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If process is already started or cannot start |
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
stop_process(handle, timeout=10.0)
Stop a multiprocessing.Process gracefully.
Signals the stop event if present to allow graceful shutdown, then attempts termination. Forces kill if process doesn't terminate within the timeout period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
RunningGrpcServer
|
RunningGrpcServer containing the process to stop |
required |
timeout
|
float
|
Maximum time to wait for graceful shutdown |
10.0
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If process cannot be stopped |
Source code in drunc/grpc_testing_tools/multiprocessing_connection_manager.py
wait_for_termination(handle, timeout=None)
Wait for multiprocessing.Process to terminate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
RunningGrpcServer
|
RunningGrpcServer to wait for |
required |
timeout
|
Optional[float]
|
Maximum time to wait |
None
|