Initialize a group of drivers. :param drivers: an iterable of drivers. :param register_finalizer: register driver.finalize method to be called at python exit. :param on_initializing: a callable to be executed BEFORE initialization. It takes the driver as the
(drivers, register_finalizer=True,
on_initializing=None, on_initialized=None, on_exception=None,
concurrent=False, dependencies=None)
| 495 | |
| 496 | |
| 497 | def initialize_many(drivers, register_finalizer=True, |
| 498 | on_initializing=None, on_initialized=None, on_exception=None, |
| 499 | concurrent=False, dependencies=None): |
| 500 | """Initialize a group of drivers. |
| 501 | |
| 502 | :param drivers: an iterable of drivers. |
| 503 | :param register_finalizer: register driver.finalize method to be called at python exit. |
| 504 | :param on_initializing: a callable to be executed BEFORE initialization. |
| 505 | It takes the driver as the first argument. |
| 506 | :param on_initialized: a callable to be executed AFTER initialization. |
| 507 | It takes the driver as the first argument. |
| 508 | :param on_exception: a callable to be executed in case an exception occurs. |
| 509 | It takes the offending driver as the first argument and the |
| 510 | exception as the second one. |
| 511 | :param concurrent: indicates that drivers with satisfied dependencies |
| 512 | should be initialized concurrently. |
| 513 | :param dependencies: indicates which drivers depend on others to be initialized. |
| 514 | each key is a driver name, and the corresponding |
| 515 | value is an iterable with its dependencies. |
| 516 | """ |
| 517 | |
| 518 | if dependencies: |
| 519 | names = {driver.name: driver for driver in drivers} |
| 520 | |
| 521 | groups = _solve_dependencies(dependencies, set(names.keys())) |
| 522 | drivers = tuple(tuple(names[name] for name in group) for group in groups) |
| 523 | for subset in drivers: |
| 524 | initialize_many(subset, register_finalizer, |
| 525 | on_initializing, on_initialized, on_exception, |
| 526 | concurrent) |
| 527 | return |
| 528 | |
| 529 | if concurrent: |
| 530 | def _finalize(d): |
| 531 | def _inner_finalize(f): |
| 532 | atexit.register(d.finalize) |
| 533 | return _inner_finalize |
| 534 | |
| 535 | def _done(d): |
| 536 | def _inner(f): |
| 537 | ex = f.exception() |
| 538 | if ex: |
| 539 | if not on_exception: |
| 540 | raise ex |
| 541 | on_exception(d, ex) |
| 542 | else: |
| 543 | if on_initialized: |
| 544 | on_initialized(d) |
| 545 | return _inner |
| 546 | |
| 547 | futs = [] |
| 548 | for driver in drivers: |
| 549 | if on_initializing: |
| 550 | on_initializing(driver) |
| 551 | fut = driver.initialize_async() |
| 552 | if register_finalizer: |
| 553 | fut.add_done_callback(_finalize(driver)) |
| 554 | fut.add_done_callback(_done(driver)) |
no test coverage detected