Pipelines provide a way to transmit multiple commands to the Redis server in one transmission. This is convenient for batch processing, such as saving all the values in a list to Redis. All commands executed within a pipeline(when running in transactional mode, which is the de
| 1662 | |
| 1663 | |
| 1664 | class Pipeline(Redis): |
| 1665 | """ |
| 1666 | Pipelines provide a way to transmit multiple commands to the Redis server |
| 1667 | in one transmission. This is convenient for batch processing, such as |
| 1668 | saving all the values in a list to Redis. |
| 1669 | |
| 1670 | All commands executed within a pipeline(when running in transactional mode, |
| 1671 | which is the default behavior) are wrapped with MULTI and EXEC |
| 1672 | calls. This guarantees all commands executed in the pipeline will be |
| 1673 | executed atomically. |
| 1674 | |
| 1675 | Any command raising an exception does *not* halt the execution of |
| 1676 | subsequent commands in the pipeline. Instead, the exception is caught |
| 1677 | and its instance is placed into the response list returned by execute(). |
| 1678 | Code iterating over the response list should be able to deal with an |
| 1679 | instance of an exception as a potential value. In general, these will be |
| 1680 | ResponseError exceptions, such as those raised when issuing a command |
| 1681 | on a key of a different datatype. |
| 1682 | """ |
| 1683 | |
| 1684 | UNWATCH_COMMANDS = {"DISCARD", "EXEC", "UNWATCH"} |
| 1685 | |
| 1686 | def __init__( |
| 1687 | self, |
| 1688 | connection_pool: ConnectionPool, |
| 1689 | response_callbacks, |
| 1690 | transaction, |
| 1691 | shard_hint, |
| 1692 | ): |
| 1693 | self.connection_pool = connection_pool |
| 1694 | self.connection: Optional[Connection] = None |
| 1695 | self.response_callbacks = response_callbacks |
| 1696 | self.transaction = transaction |
| 1697 | self.shard_hint = shard_hint |
| 1698 | self.watching = False |
| 1699 | self.command_stack = [] |
| 1700 | self.scripts: Set[Script] = set() |
| 1701 | self.explicit_transaction = False |
| 1702 | |
| 1703 | def __enter__(self) -> "Pipeline": |
| 1704 | return self |
| 1705 | |
| 1706 | def __exit__(self, exc_type, exc_value, traceback): |
| 1707 | self.reset() |
| 1708 | |
| 1709 | def __del__(self): |
| 1710 | try: |
| 1711 | self.reset() |
| 1712 | except Exception: |
| 1713 | pass |
| 1714 | |
| 1715 | def __len__(self) -> int: |
| 1716 | return len(self.command_stack) |
| 1717 | |
| 1718 | def __bool__(self) -> bool: |
| 1719 | """Pipeline instances should always evaluate to True""" |
| 1720 | return True |
| 1721 |
no outgoing calls