pandas_openscm.parallelisation#
Parallelisation helpers
Parallelisation is notoriously difficult. Here we capture some helpers that we have found useful. If you want to get started with parallelisation, have a look at some of these resources:
- https://britishgeologicalsurvey.github.io/science/python-forking-vs-spawn/
- https://pythonspeed.com/articles/python-multiprocessing/
- https://joblib.readthedocs.io/en/latest/parallel.html
If you want to understand why I've gone with concurrent.futures as the default, have a look at this excellent Stack Overflow answer (tl;dr - concurrent.futures does enough, the added complexity of the lower-level interfaces isn't worth it).
Classes:
| Name | Description |
|---|---|
ParallelOpConfig |
Configuration for a potentially parallel op, potentially with a progress bar(s) |
ProgressLike |
A callable that acts like something which creates a progress bar |
Functions:
| Name | Description |
|---|---|
apply_op_parallel_progress |
Apply an operation, potentially in parallel and potentially with progress bars |
get_tqdm_auto |
Get a progress bar from tqdm.auto. |
ParallelOpConfig #
Configuration for a potentially parallel op, potentially with a progress bar(s)
Methods:
| Name | Description |
|---|---|
from_user_facing |
Initialise from more user-facing arguments |
Attributes:
| Name | Type | Description |
|---|---|---|
executor |
Executor | None
|
Executor with which to perform the op. |
executor_created_in_class_method |
bool
|
Whether |
progress_parallel_submission |
ProgressLike | None
|
Progress bar to track the submission of the iterable to the parallel executor. |
progress_results |
ProgressLike | None
|
Progress bar to track the results of the op. |
Source code in src/pandas_openscm/parallelisation.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
executor
class-attribute
instance-attribute
#
executor: Executor | None = None
Executor with which to perform the op.
If None, the op is executed serially.
executor_created_in_class_method
class-attribute
instance-attribute
#
executor_created_in_class_method: bool = False
Whether self.executor was created in a class method
This can be used to indicate that the user needs to shutdown the executor, it was not created from an accessible place.
progress_parallel_submission
class-attribute
instance-attribute
#
progress_parallel_submission: ProgressLike | None = None
Progress bar to track the submission of the iterable to the parallel executor.
If None, no progress bar is displayed for the results of the op.
progress_results
class-attribute
instance-attribute
#
progress_results: ProgressLike | None = None
Progress bar to track the results of the op.
If None, no progress bar is displayed for the results of the op.
from_user_facing
classmethod
#
from_user_facing(
progress: bool = False,
progress_results_kwargs: dict[str, Any] | None = None,
progress_parallel_submission_kwargs: dict[str, Any]
| None = None,
max_workers: int | None = None,
parallel_pool_cls: type[ProcessPoolExecutor]
| type[ThreadPoolExecutor] = ProcessPoolExecutor,
) -> ParallelOpConfig
Initialise from more user-facing arguments
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress
|
bool
|
Should we show progress bars? |
False
|
progress_results_kwargs
|
dict[str, Any] | None
|
Passed to get_tqdm_auto when creating the results bar. Only used if |
None
|
progress_parallel_submission_kwargs
|
dict[str, Any] | None
|
Passed to get_tqdm_auto when creating the parallel submission bar. Only used if |
None
|
max_workers
|
int | None
|
Maximum number of parallel workers. If |
None
|
parallel_pool_cls
|
type[ProcessPoolExecutor] | type[ThreadPoolExecutor]
|
Type of parallel pool executor to use if |
ProcessPoolExecutor
|
Returns:
| Type | Description |
|---|---|
ParallelOpConfig
|
Initialised instance. |
Source code in src/pandas_openscm/parallelisation.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
ProgressLike #
Bases: Protocol
A callable that acts like something which creates a progress bar
Methods:
| Name | Description |
|---|---|
__call__ |
Create the progress bar |
Source code in src/pandas_openscm/parallelisation.py
apply_op_parallel_progress #
apply_op_parallel_progress(
func_to_call: Callable[Concatenate[U, P], T],
iterable_input: Iterable[U],
parallel_op_config: ParallelOpConfig,
*args: args,
**kwargs: kwargs,
) -> tuple[T, ...]
Apply an operation, potentially in parallel and potentially with progress bars
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_to_call
|
Callable[Concatenate[U, P], T]
|
Operation to apply to (i.e. the function to call on) |
required |
iterable_input
|
Iterable[U]
|
The input on which to perform Each element of |
required |
parallel_op_config
|
ParallelOpConfig
|
Configuration with which to execute the potentially parallel process |
required |
*args
|
args
|
Passed to each call to |
()
|
**kwargs
|
kwargs
|
Passed to each call to |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[T, ...]
|
Results of each call to |
Notes
This function does not handle nested progress bars in an obvious way. They are possible, but you'll need to set the bar creation stuff up appropriately in the layer above calling this function (this function doesn't need to know about the location of the bars, it just uses whatever bars it is given).
This function also doesn't handle keeping track of order
i.e. which outputs map to which inputs.
If you need this, either add the injection suggested in the code below
or create another function(
e.g. create apply_op_parallel_progress_output_tracker).
Our suggestion would be to do the latter first
and only switch to injection if we find we need more flexibility,
because the abstractions will likely become hard to follow.
Source code in src/pandas_openscm/parallelisation.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
get_tqdm_auto #
get_tqdm_auto(**kwargs: Any) -> ProgressLike
Get a progress bar from tqdm.auto.
Strictly speaking, we use tqdm.auto.tqdm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
ProgressLike
|
Default progress bar. |
Notes
This abstraction isn't stricly necessary. It's helpful for having this in one place and also to only have to do the type hinting fix in one place.