Convenience method for executing the callable `func` as a transaction while watching all keys specified in `watches`. The 'func' callable should expect a single argument which is a Pipeline object.
(
self, func: Callable[["Pipeline"], None], *watches, **kwargs
)
| 579 | ) |
| 580 | |
| 581 | def transaction( |
| 582 | self, func: Callable[["Pipeline"], None], *watches, **kwargs |
| 583 | ) -> Union[List[Any], Any, None]: |
| 584 | """ |
| 585 | Convenience method for executing the callable `func` as a transaction |
| 586 | while watching all keys specified in `watches`. The 'func' callable |
| 587 | should expect a single argument which is a Pipeline object. |
| 588 | """ |
| 589 | shard_hint = kwargs.pop("shard_hint", None) |
| 590 | value_from_callable = kwargs.pop("value_from_callable", False) |
| 591 | watch_delay = kwargs.pop("watch_delay", None) |
| 592 | with self.pipeline(True, shard_hint) as pipe: |
| 593 | while True: |
| 594 | try: |
| 595 | if watches: |
| 596 | pipe.watch(*watches) |
| 597 | func_value = func(pipe) |
| 598 | exec_value = pipe.execute() |
| 599 | return func_value if value_from_callable else exec_value |
| 600 | except WatchError: |
| 601 | if watch_delay is not None and watch_delay > 0: |
| 602 | time.sleep(watch_delay) |
| 603 | continue |
| 604 | |
| 605 | def lock( |
| 606 | self, |