图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1552 lines
58 KiB

  1. import asyncio
  2. import copy
  3. import inspect
  4. import re
  5. import ssl
  6. import warnings
  7. from typing import (
  8. TYPE_CHECKING,
  9. Any,
  10. AsyncIterator,
  11. Awaitable,
  12. Callable,
  13. Dict,
  14. Iterable,
  15. List,
  16. Mapping,
  17. MutableMapping,
  18. Optional,
  19. Set,
  20. Tuple,
  21. Type,
  22. TypeVar,
  23. Union,
  24. cast,
  25. )
  26. from redis._parsers.helpers import (
  27. _RedisCallbacks,
  28. _RedisCallbacksRESP2,
  29. _RedisCallbacksRESP3,
  30. bool_ok,
  31. )
  32. from redis.asyncio.connection import (
  33. Connection,
  34. ConnectionPool,
  35. SSLConnection,
  36. UnixDomainSocketConnection,
  37. )
  38. from redis.asyncio.lock import Lock
  39. from redis.asyncio.retry import Retry
  40. from redis.client import (
  41. EMPTY_RESPONSE,
  42. NEVER_DECODE,
  43. AbstractRedis,
  44. CaseInsensitiveDict,
  45. )
  46. from redis.commands import (
  47. AsyncCoreCommands,
  48. AsyncRedisModuleCommands,
  49. AsyncSentinelCommands,
  50. list_or_args,
  51. )
  52. from redis.compat import Protocol, TypedDict
  53. from redis.credentials import CredentialProvider
  54. from redis.exceptions import (
  55. ConnectionError,
  56. ExecAbortError,
  57. PubSubError,
  58. RedisError,
  59. ResponseError,
  60. TimeoutError,
  61. WatchError,
  62. )
  63. from redis.typing import ChannelT, EncodableT, KeyT
  64. from redis.utils import (
  65. HIREDIS_AVAILABLE,
  66. _set_info_logger,
  67. deprecated_function,
  68. get_lib_version,
  69. safe_str,
  70. str_if_bytes,
  71. )
  72. PubSubHandler = Callable[[Dict[str, str]], Awaitable[None]]
  73. _KeyT = TypeVar("_KeyT", bound=KeyT)
  74. _ArgT = TypeVar("_ArgT", KeyT, EncodableT)
  75. _RedisT = TypeVar("_RedisT", bound="Redis")
  76. _NormalizeKeysT = TypeVar("_NormalizeKeysT", bound=Mapping[ChannelT, object])
  77. if TYPE_CHECKING:
  78. from redis.commands.core import Script
  79. class ResponseCallbackProtocol(Protocol):
  80. def __call__(self, response: Any, **kwargs):
  81. ...
  82. class AsyncResponseCallbackProtocol(Protocol):
  83. async def __call__(self, response: Any, **kwargs):
  84. ...
  85. ResponseCallbackT = Union[ResponseCallbackProtocol, AsyncResponseCallbackProtocol]
  86. class Redis(
  87. AbstractRedis, AsyncRedisModuleCommands, AsyncCoreCommands, AsyncSentinelCommands
  88. ):
  89. """
  90. Implementation of the Redis protocol.
  91. This abstract class provides a Python interface to all Redis commands
  92. and an implementation of the Redis protocol.
  93. Pipelines derive from this, implementing how
  94. the commands are sent and received to the Redis server. Based on
  95. configuration, an instance will either use a ConnectionPool, or
  96. Connection object to talk to redis.
  97. """
  98. response_callbacks: MutableMapping[Union[str, bytes], ResponseCallbackT]
  99. @classmethod
  100. def from_url(
  101. cls,
  102. url: str,
  103. single_connection_client: bool = False,
  104. auto_close_connection_pool: Optional[bool] = None,
  105. **kwargs,
  106. ):
  107. """
  108. Return a Redis client object configured from the given URL
  109. For example::
  110. redis://[[username]:[password]]@localhost:6379/0
  111. rediss://[[username]:[password]]@localhost:6379/0
  112. unix://[username@]/path/to/socket.sock?db=0[&password=password]
  113. Three URL schemes are supported:
  114. - `redis://` creates a TCP socket connection. See more at:
  115. <https://www.iana.org/assignments/uri-schemes/prov/redis>
  116. - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
  117. <https://www.iana.org/assignments/uri-schemes/prov/rediss>
  118. - ``unix://``: creates a Unix Domain Socket connection.
  119. The username, password, hostname, path and all querystring values
  120. are passed through urllib.parse.unquote in order to replace any
  121. percent-encoded values with their corresponding characters.
  122. There are several ways to specify a database number. The first value
  123. found will be used:
  124. 1. A ``db`` querystring option, e.g. redis://localhost?db=0
  125. 2. If using the redis:// or rediss:// schemes, the path argument
  126. of the url, e.g. redis://localhost/0
  127. 3. A ``db`` keyword argument to this function.
  128. If none of these options are specified, the default db=0 is used.
  129. All querystring options are cast to their appropriate Python types.
  130. Boolean arguments can be specified with string values "True"/"False"
  131. or "Yes"/"No". Values that cannot be properly cast cause a
  132. ``ValueError`` to be raised. Once parsed, the querystring arguments
  133. and keyword arguments are passed to the ``ConnectionPool``'s
  134. class initializer. In the case of conflicting arguments, querystring
  135. arguments always win.
  136. """
  137. connection_pool = ConnectionPool.from_url(url, **kwargs)
  138. client = cls(
  139. connection_pool=connection_pool,
  140. single_connection_client=single_connection_client,
  141. )
  142. if auto_close_connection_pool is not None:
  143. warnings.warn(
  144. DeprecationWarning(
  145. '"auto_close_connection_pool" is deprecated '
  146. "since version 5.0.1. "
  147. "Please create a ConnectionPool explicitly and "
  148. "provide to the Redis() constructor instead."
  149. )
  150. )
  151. else:
  152. auto_close_connection_pool = True
  153. client.auto_close_connection_pool = auto_close_connection_pool
  154. return client
  155. @classmethod
  156. def from_pool(
  157. cls: Type["Redis"],
  158. connection_pool: ConnectionPool,
  159. ) -> "Redis":
  160. """
  161. Return a Redis client from the given connection pool.
  162. The Redis client will take ownership of the connection pool and
  163. close it when the Redis client is closed.
  164. """
  165. client = cls(
  166. connection_pool=connection_pool,
  167. )
  168. client.auto_close_connection_pool = True
  169. return client
  170. def __init__(
  171. self,
  172. *,
  173. host: str = "localhost",
  174. port: int = 6379,
  175. db: Union[str, int] = 0,
  176. password: Optional[str] = None,
  177. socket_timeout: Optional[float] = None,
  178. socket_connect_timeout: Optional[float] = None,
  179. socket_keepalive: Optional[bool] = None,
  180. socket_keepalive_options: Optional[Mapping[int, Union[int, bytes]]] = None,
  181. connection_pool: Optional[ConnectionPool] = None,
  182. unix_socket_path: Optional[str] = None,
  183. encoding: str = "utf-8",
  184. encoding_errors: str = "strict",
  185. decode_responses: bool = False,
  186. retry_on_timeout: bool = False,
  187. retry_on_error: Optional[list] = None,
  188. ssl: bool = False,
  189. ssl_keyfile: Optional[str] = None,
  190. ssl_certfile: Optional[str] = None,
  191. ssl_cert_reqs: str = "required",
  192. ssl_ca_certs: Optional[str] = None,
  193. ssl_ca_data: Optional[str] = None,
  194. ssl_check_hostname: bool = False,
  195. ssl_min_version: Optional[ssl.TLSVersion] = None,
  196. ssl_ciphers: Optional[str] = None,
  197. max_connections: Optional[int] = None,
  198. single_connection_client: bool = False,
  199. health_check_interval: int = 0,
  200. client_name: Optional[str] = None,
  201. lib_name: Optional[str] = "redis-py",
  202. lib_version: Optional[str] = get_lib_version(),
  203. username: Optional[str] = None,
  204. retry: Optional[Retry] = None,
  205. auto_close_connection_pool: Optional[bool] = None,
  206. redis_connect_func=None,
  207. credential_provider: Optional[CredentialProvider] = None,
  208. protocol: Optional[int] = 2,
  209. ):
  210. """
  211. Initialize a new Redis client.
  212. To specify a retry policy for specific errors, first set
  213. `retry_on_error` to a list of the error/s to retry on, then set
  214. `retry` to a valid `Retry` object.
  215. To retry on TimeoutError, `retry_on_timeout` can also be set to `True`.
  216. """
  217. kwargs: Dict[str, Any]
  218. # auto_close_connection_pool only has an effect if connection_pool is
  219. # None. It is assumed that if connection_pool is not None, the user
  220. # wants to manage the connection pool themselves.
  221. if auto_close_connection_pool is not None:
  222. warnings.warn(
  223. DeprecationWarning(
  224. '"auto_close_connection_pool" is deprecated '
  225. "since version 5.0.1. "
  226. "Please create a ConnectionPool explicitly and "
  227. "provide to the Redis() constructor instead."
  228. )
  229. )
  230. else:
  231. auto_close_connection_pool = True
  232. if not connection_pool:
  233. # Create internal connection pool, expected to be closed by Redis instance
  234. if not retry_on_error:
  235. retry_on_error = []
  236. if retry_on_timeout is True:
  237. retry_on_error.append(TimeoutError)
  238. kwargs = {
  239. "db": db,
  240. "username": username,
  241. "password": password,
  242. "credential_provider": credential_provider,
  243. "socket_timeout": socket_timeout,
  244. "encoding": encoding,
  245. "encoding_errors": encoding_errors,
  246. "decode_responses": decode_responses,
  247. "retry_on_timeout": retry_on_timeout,
  248. "retry_on_error": retry_on_error,
  249. "retry": copy.deepcopy(retry),
  250. "max_connections": max_connections,
  251. "health_check_interval": health_check_interval,
  252. "client_name": client_name,
  253. "lib_name": lib_name,
  254. "lib_version": lib_version,
  255. "redis_connect_func": redis_connect_func,
  256. "protocol": protocol,
  257. }
  258. # based on input, setup appropriate connection args
  259. if unix_socket_path is not None:
  260. kwargs.update(
  261. {
  262. "path": unix_socket_path,
  263. "connection_class": UnixDomainSocketConnection,
  264. }
  265. )
  266. else:
  267. # TCP specific options
  268. kwargs.update(
  269. {
  270. "host": host,
  271. "port": port,
  272. "socket_connect_timeout": socket_connect_timeout,
  273. "socket_keepalive": socket_keepalive,
  274. "socket_keepalive_options": socket_keepalive_options,
  275. }
  276. )
  277. if ssl:
  278. kwargs.update(
  279. {
  280. "connection_class": SSLConnection,
  281. "ssl_keyfile": ssl_keyfile,
  282. "ssl_certfile": ssl_certfile,
  283. "ssl_cert_reqs": ssl_cert_reqs,
  284. "ssl_ca_certs": ssl_ca_certs,
  285. "ssl_ca_data": ssl_ca_data,
  286. "ssl_check_hostname": ssl_check_hostname,
  287. "ssl_min_version": ssl_min_version,
  288. "ssl_ciphers": ssl_ciphers,
  289. }
  290. )
  291. # This arg only used if no pool is passed in
  292. self.auto_close_connection_pool = auto_close_connection_pool
  293. connection_pool = ConnectionPool(**kwargs)
  294. else:
  295. # If a pool is passed in, do not close it
  296. self.auto_close_connection_pool = False
  297. self.connection_pool = connection_pool
  298. self.single_connection_client = single_connection_client
  299. self.connection: Optional[Connection] = None
  300. self.response_callbacks = CaseInsensitiveDict(_RedisCallbacks)
  301. if self.connection_pool.connection_kwargs.get("protocol") in ["3", 3]:
  302. self.response_callbacks.update(_RedisCallbacksRESP3)
  303. else:
  304. self.response_callbacks.update(_RedisCallbacksRESP2)
  305. # If using a single connection client, we need to lock creation-of and use-of
  306. # the client in order to avoid race conditions such as using asyncio.gather
  307. # on a set of redis commands
  308. self._single_conn_lock = asyncio.Lock()
  309. def __repr__(self):
  310. return (
  311. f"<{self.__class__.__module__}.{self.__class__.__name__}"
  312. f"({self.connection_pool!r})>"
  313. )
  314. def __await__(self):
  315. return self.initialize().__await__()
  316. async def initialize(self: _RedisT) -> _RedisT:
  317. if self.single_connection_client:
  318. async with self._single_conn_lock:
  319. if self.connection is None:
  320. self.connection = await self.connection_pool.get_connection("_")
  321. return self
  322. def set_response_callback(self, command: str, callback: ResponseCallbackT):
  323. """Set a custom Response Callback"""
  324. self.response_callbacks[command] = callback
  325. def get_encoder(self):
  326. """Get the connection pool's encoder"""
  327. return self.connection_pool.get_encoder()
  328. def get_connection_kwargs(self):
  329. """Get the connection's key-word arguments"""
  330. return self.connection_pool.connection_kwargs
  331. def get_retry(self) -> Optional["Retry"]:
  332. return self.get_connection_kwargs().get("retry")
  333. def set_retry(self, retry: "Retry") -> None:
  334. self.get_connection_kwargs().update({"retry": retry})
  335. self.connection_pool.set_retry(retry)
  336. def load_external_module(self, funcname, func):
  337. """
  338. This function can be used to add externally defined redis modules,
  339. and their namespaces to the redis client.
  340. funcname - A string containing the name of the function to create
  341. func - The function, being added to this class.
  342. ex: Assume that one has a custom redis module named foomod that
  343. creates command named 'foo.dothing' and 'foo.anotherthing' in redis.
  344. To load function functions into this namespace:
  345. from redis import Redis
  346. from foomodule import F
  347. r = Redis()
  348. r.load_external_module("foo", F)
  349. r.foo().dothing('your', 'arguments')
  350. For a concrete example see the reimport of the redisjson module in
  351. tests/test_connection.py::test_loading_external_modules
  352. """
  353. setattr(self, funcname, func)
  354. def pipeline(
  355. self, transaction: bool = True, shard_hint: Optional[str] = None
  356. ) -> "Pipeline":
  357. """
  358. Return a new pipeline object that can queue multiple commands for
  359. later execution. ``transaction`` indicates whether all commands
  360. should be executed atomically. Apart from making a group of operations
  361. atomic, pipelines are useful for reducing the back-and-forth overhead
  362. between the client and server.
  363. """
  364. return Pipeline(
  365. self.connection_pool, self.response_callbacks, transaction, shard_hint
  366. )
  367. async def transaction(
  368. self,
  369. func: Callable[["Pipeline"], Union[Any, Awaitable[Any]]],
  370. *watches: KeyT,
  371. shard_hint: Optional[str] = None,
  372. value_from_callable: bool = False,
  373. watch_delay: Optional[float] = None,
  374. ):
  375. """
  376. Convenience method for executing the callable `func` as a transaction
  377. while watching all keys specified in `watches`. The 'func' callable
  378. should expect a single argument which is a Pipeline object.
  379. """
  380. pipe: Pipeline
  381. async with self.pipeline(True, shard_hint) as pipe:
  382. while True:
  383. try:
  384. if watches:
  385. await pipe.watch(*watches)
  386. func_value = func(pipe)
  387. if inspect.isawaitable(func_value):
  388. func_value = await func_value
  389. exec_value = await pipe.execute()
  390. return func_value if value_from_callable else exec_value
  391. except WatchError:
  392. if watch_delay is not None and watch_delay > 0:
  393. await asyncio.sleep(watch_delay)
  394. continue
  395. def lock(
  396. self,
  397. name: KeyT,
  398. timeout: Optional[float] = None,
  399. sleep: float = 0.1,
  400. blocking: bool = True,
  401. blocking_timeout: Optional[float] = None,
  402. lock_class: Optional[Type[Lock]] = None,
  403. thread_local: bool = True,
  404. ) -> Lock:
  405. """
  406. Return a new Lock object using key ``name`` that mimics
  407. the behavior of threading.Lock.
  408. If specified, ``timeout`` indicates a maximum life for the lock.
  409. By default, it will remain locked until release() is called.
  410. ``sleep`` indicates the amount of time to sleep per loop iteration
  411. when the lock is in blocking mode and another client is currently
  412. holding the lock.
  413. ``blocking`` indicates whether calling ``acquire`` should block until
  414. the lock has been acquired or to fail immediately, causing ``acquire``
  415. to return False and the lock not being acquired. Defaults to True.
  416. Note this value can be overridden by passing a ``blocking``
  417. argument to ``acquire``.
  418. ``blocking_timeout`` indicates the maximum amount of time in seconds to
  419. spend trying to acquire the lock. A value of ``None`` indicates
  420. continue trying forever. ``blocking_timeout`` can be specified as a
  421. float or integer, both representing the number of seconds to wait.
  422. ``lock_class`` forces the specified lock implementation. Note that as
  423. of redis-py 3.0, the only lock class we implement is ``Lock`` (which is
  424. a Lua-based lock). So, it's unlikely you'll need this parameter, unless
  425. you have created your own custom lock class.
  426. ``thread_local`` indicates whether the lock token is placed in
  427. thread-local storage. By default, the token is placed in thread local
  428. storage so that a thread only sees its token, not a token set by
  429. another thread. Consider the following timeline:
  430. time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
  431. thread-1 sets the token to "abc"
  432. time: 1, thread-2 blocks trying to acquire `my-lock` using the
  433. Lock instance.
  434. time: 5, thread-1 has not yet completed. redis expires the lock
  435. key.
  436. time: 5, thread-2 acquired `my-lock` now that it's available.
  437. thread-2 sets the token to "xyz"
  438. time: 6, thread-1 finishes its work and calls release(). if the
  439. token is *not* stored in thread local storage, then
  440. thread-1 would see the token value as "xyz" and would be
  441. able to successfully release the thread-2's lock.
  442. In some use cases it's necessary to disable thread local storage. For
  443. example, if you have code where one thread acquires a lock and passes
  444. that lock instance to a worker thread to release later. If thread
  445. local storage isn't disabled in this case, the worker thread won't see
  446. the token set by the thread that acquired the lock. Our assumption
  447. is that these cases aren't common and as such default to using
  448. thread local storage."""
  449. if lock_class is None:
  450. lock_class = Lock
  451. return lock_class(
  452. self,
  453. name,
  454. timeout=timeout,
  455. sleep=sleep,
  456. blocking=blocking,
  457. blocking_timeout=blocking_timeout,
  458. thread_local=thread_local,
  459. )
  460. def pubsub(self, **kwargs) -> "PubSub":
  461. """
  462. Return a Publish/Subscribe object. With this object, you can
  463. subscribe to channels and listen for messages that get published to
  464. them.
  465. """
  466. return PubSub(self.connection_pool, **kwargs)
  467. def monitor(self) -> "Monitor":
  468. return Monitor(self.connection_pool)
  469. def client(self) -> "Redis":
  470. return self.__class__(
  471. connection_pool=self.connection_pool, single_connection_client=True
  472. )
  473. async def __aenter__(self: _RedisT) -> _RedisT:
  474. return await self.initialize()
  475. async def __aexit__(self, exc_type, exc_value, traceback):
  476. await self.aclose()
  477. _DEL_MESSAGE = "Unclosed Redis client"
  478. # passing _warnings and _grl as argument default since they may be gone
  479. # by the time __del__ is called at shutdown
  480. def __del__(
  481. self,
  482. _warn: Any = warnings.warn,
  483. _grl: Any = asyncio.get_running_loop,
  484. ) -> None:
  485. if hasattr(self, "connection") and (self.connection is not None):
  486. _warn(f"Unclosed client session {self!r}", ResourceWarning, source=self)
  487. try:
  488. context = {"client": self, "message": self._DEL_MESSAGE}
  489. _grl().call_exception_handler(context)
  490. except RuntimeError:
  491. pass
  492. self.connection._close()
  493. async def aclose(self, close_connection_pool: Optional[bool] = None) -> None:
  494. """
  495. Closes Redis client connection
  496. :param close_connection_pool: decides whether to close the connection pool used
  497. by this Redis client, overriding Redis.auto_close_connection_pool. By default,
  498. let Redis.auto_close_connection_pool decide whether to close the connection
  499. pool.
  500. """
  501. conn = self.connection
  502. if conn:
  503. self.connection = None
  504. await self.connection_pool.release(conn)
  505. if close_connection_pool or (
  506. close_connection_pool is None and self.auto_close_connection_pool
  507. ):
  508. await self.connection_pool.disconnect()
  509. @deprecated_function(version="5.0.1", reason="Use aclose() instead", name="close")
  510. async def close(self, close_connection_pool: Optional[bool] = None) -> None:
  511. """
  512. Alias for aclose(), for backwards compatibility
  513. """
  514. await self.aclose(close_connection_pool)
  515. async def _send_command_parse_response(self, conn, command_name, *args, **options):
  516. """
  517. Send a command and parse the response
  518. """
  519. await conn.send_command(*args)
  520. return await self.parse_response(conn, command_name, **options)
  521. async def _disconnect_raise(self, conn: Connection, error: Exception):
  522. """
  523. Close the connection and raise an exception
  524. if retry_on_error is not set or the error
  525. is not one of the specified error types
  526. """
  527. await conn.disconnect()
  528. if (
  529. conn.retry_on_error is None
  530. or isinstance(error, tuple(conn.retry_on_error)) is False
  531. ):
  532. raise error
  533. # COMMAND EXECUTION AND PROTOCOL PARSING
  534. async def execute_command(self, *args, **options):
  535. """Execute a command and return a parsed response"""
  536. await self.initialize()
  537. pool = self.connection_pool
  538. command_name = args[0]
  539. conn = self.connection or await pool.get_connection(command_name, **options)
  540. if self.single_connection_client:
  541. await self._single_conn_lock.acquire()
  542. try:
  543. return await conn.retry.call_with_retry(
  544. lambda: self._send_command_parse_response(
  545. conn, command_name, *args, **options
  546. ),
  547. lambda error: self._disconnect_raise(conn, error),
  548. )
  549. finally:
  550. if self.single_connection_client:
  551. self._single_conn_lock.release()
  552. if not self.connection:
  553. await pool.release(conn)
  554. async def parse_response(
  555. self, connection: Connection, command_name: Union[str, bytes], **options
  556. ):
  557. """Parses a response from the Redis server"""
  558. try:
  559. if NEVER_DECODE in options:
  560. response = await connection.read_response(disable_decoding=True)
  561. options.pop(NEVER_DECODE)
  562. else:
  563. response = await connection.read_response()
  564. except ResponseError:
  565. if EMPTY_RESPONSE in options:
  566. return options[EMPTY_RESPONSE]
  567. raise
  568. if EMPTY_RESPONSE in options:
  569. options.pop(EMPTY_RESPONSE)
  570. if command_name in self.response_callbacks:
  571. # Mypy bug: https://github.com/python/mypy/issues/10977
  572. command_name = cast(str, command_name)
  573. retval = self.response_callbacks[command_name](response, **options)
  574. return await retval if inspect.isawaitable(retval) else retval
  575. return response
  576. StrictRedis = Redis
  577. class MonitorCommandInfo(TypedDict):
  578. time: float
  579. db: int
  580. client_address: str
  581. client_port: str
  582. client_type: str
  583. command: str
  584. class Monitor:
  585. """
  586. Monitor is useful for handling the MONITOR command to the redis server.
  587. next_command() method returns one command from monitor
  588. listen() method yields commands from monitor.
  589. """
  590. monitor_re = re.compile(r"\[(\d+) (.*?)\] (.*)")
  591. command_re = re.compile(r'"(.*?)(?<!\\)"')
  592. def __init__(self, connection_pool: ConnectionPool):
  593. self.connection_pool = connection_pool
  594. self.connection: Optional[Connection] = None
  595. async def connect(self):
  596. if self.connection is None:
  597. self.connection = await self.connection_pool.get_connection("MONITOR")
  598. async def __aenter__(self):
  599. await self.connect()
  600. await self.connection.send_command("MONITOR")
  601. # check that monitor returns 'OK', but don't return it to user
  602. response = await self.connection.read_response()
  603. if not bool_ok(response):
  604. raise RedisError(f"MONITOR failed: {response}")
  605. return self
  606. async def __aexit__(self, *args):
  607. await self.connection.disconnect()
  608. await self.connection_pool.release(self.connection)
  609. async def next_command(self) -> MonitorCommandInfo:
  610. """Parse the response from a monitor command"""
  611. await self.connect()
  612. response = await self.connection.read_response()
  613. if isinstance(response, bytes):
  614. response = self.connection.encoder.decode(response, force=True)
  615. command_time, command_data = response.split(" ", 1)
  616. m = self.monitor_re.match(command_data)
  617. db_id, client_info, command = m.groups()
  618. command = " ".join(self.command_re.findall(command))
  619. # Redis escapes double quotes because each piece of the command
  620. # string is surrounded by double quotes. We don't have that
  621. # requirement so remove the escaping and leave the quote.
  622. command = command.replace('\\"', '"')
  623. if client_info == "lua":
  624. client_address = "lua"
  625. client_port = ""
  626. client_type = "lua"
  627. elif client_info.startswith("unix"):
  628. client_address = "unix"
  629. client_port = client_info[5:]
  630. client_type = "unix"
  631. else:
  632. # use rsplit as ipv6 addresses contain colons
  633. client_address, client_port = client_info.rsplit(":", 1)
  634. client_type = "tcp"
  635. return {
  636. "time": float(command_time),
  637. "db": int(db_id),
  638. "client_address": client_address,
  639. "client_port": client_port,
  640. "client_type": client_type,
  641. "command": command,
  642. }
  643. async def listen(self) -> AsyncIterator[MonitorCommandInfo]:
  644. """Listen for commands coming to the server."""
  645. while True:
  646. yield await self.next_command()
  647. class PubSub:
  648. """
  649. PubSub provides publish, subscribe and listen support to Redis channels.
  650. After subscribing to one or more channels, the listen() method will block
  651. until a message arrives on one of the subscribed channels. That message
  652. will be returned and it's safe to start listening again.
  653. """
  654. PUBLISH_MESSAGE_TYPES = ("message", "pmessage")
  655. UNSUBSCRIBE_MESSAGE_TYPES = ("unsubscribe", "punsubscribe")
  656. HEALTH_CHECK_MESSAGE = "redis-py-health-check"
  657. def __init__(
  658. self,
  659. connection_pool: ConnectionPool,
  660. shard_hint: Optional[str] = None,
  661. ignore_subscribe_messages: bool = False,
  662. encoder=None,
  663. push_handler_func: Optional[Callable] = None,
  664. ):
  665. self.connection_pool = connection_pool
  666. self.shard_hint = shard_hint
  667. self.ignore_subscribe_messages = ignore_subscribe_messages
  668. self.connection = None
  669. # we need to know the encoding options for this connection in order
  670. # to lookup channel and pattern names for callback handlers.
  671. self.encoder = encoder
  672. self.push_handler_func = push_handler_func
  673. if self.encoder is None:
  674. self.encoder = self.connection_pool.get_encoder()
  675. if self.encoder.decode_responses:
  676. self.health_check_response = [
  677. ["pong", self.HEALTH_CHECK_MESSAGE],
  678. self.HEALTH_CHECK_MESSAGE,
  679. ]
  680. else:
  681. self.health_check_response = [
  682. [b"pong", self.encoder.encode(self.HEALTH_CHECK_MESSAGE)],
  683. self.encoder.encode(self.HEALTH_CHECK_MESSAGE),
  684. ]
  685. if self.push_handler_func is None:
  686. _set_info_logger()
  687. self.channels = {}
  688. self.pending_unsubscribe_channels = set()
  689. self.patterns = {}
  690. self.pending_unsubscribe_patterns = set()
  691. self._lock = asyncio.Lock()
  692. async def __aenter__(self):
  693. return self
  694. async def __aexit__(self, exc_type, exc_value, traceback):
  695. await self.aclose()
  696. def __del__(self):
  697. if self.connection:
  698. self.connection.deregister_connect_callback(self.on_connect)
  699. async def aclose(self):
  700. # In case a connection property does not yet exist
  701. # (due to a crash earlier in the Redis() constructor), return
  702. # immediately as there is nothing to clean-up.
  703. if not hasattr(self, "connection"):
  704. return
  705. async with self._lock:
  706. if self.connection:
  707. await self.connection.disconnect()
  708. self.connection.deregister_connect_callback(self.on_connect)
  709. await self.connection_pool.release(self.connection)
  710. self.connection = None
  711. self.channels = {}
  712. self.pending_unsubscribe_channels = set()
  713. self.patterns = {}
  714. self.pending_unsubscribe_patterns = set()
  715. @deprecated_function(version="5.0.1", reason="Use aclose() instead", name="close")
  716. async def close(self) -> None:
  717. """Alias for aclose(), for backwards compatibility"""
  718. await self.aclose()
  719. @deprecated_function(version="5.0.1", reason="Use aclose() instead", name="reset")
  720. async def reset(self) -> None:
  721. """Alias for aclose(), for backwards compatibility"""
  722. await self.aclose()
  723. async def on_connect(self, connection: Connection):
  724. """Re-subscribe to any channels and patterns previously subscribed to"""
  725. # NOTE: for python3, we can't pass bytestrings as keyword arguments
  726. # so we need to decode channel/pattern names back to unicode strings
  727. # before passing them to [p]subscribe.
  728. self.pending_unsubscribe_channels.clear()
  729. self.pending_unsubscribe_patterns.clear()
  730. if self.channels:
  731. channels = {}
  732. for k, v in self.channels.items():
  733. channels[self.encoder.decode(k, force=True)] = v
  734. await self.subscribe(**channels)
  735. if self.patterns:
  736. patterns = {}
  737. for k, v in self.patterns.items():
  738. patterns[self.encoder.decode(k, force=True)] = v
  739. await self.psubscribe(**patterns)
  740. @property
  741. def subscribed(self):
  742. """Indicates if there are subscriptions to any channels or patterns"""
  743. return bool(self.channels or self.patterns)
  744. async def execute_command(self, *args: EncodableT):
  745. """Execute a publish/subscribe command"""
  746. # NOTE: don't parse the response in this function -- it could pull a
  747. # legitimate message off the stack if the connection is already
  748. # subscribed to one or more channels
  749. await self.connect()
  750. connection = self.connection
  751. kwargs = {"check_health": not self.subscribed}
  752. await self._execute(connection, connection.send_command, *args, **kwargs)
  753. async def connect(self):
  754. """
  755. Ensure that the PubSub is connected
  756. """
  757. if self.connection is None:
  758. self.connection = await self.connection_pool.get_connection(
  759. "pubsub", self.shard_hint
  760. )
  761. # register a callback that re-subscribes to any channels we
  762. # were listening to when we were disconnected
  763. self.connection.register_connect_callback(self.on_connect)
  764. else:
  765. await self.connection.connect()
  766. if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
  767. self.connection._parser.set_push_handler(self.push_handler_func)
  768. async def _disconnect_raise_connect(self, conn, error):
  769. """
  770. Close the connection and raise an exception
  771. if retry_on_error is not set or the error is not one
  772. of the specified error types. Otherwise, try to
  773. reconnect
  774. """
  775. await conn.disconnect()
  776. if (
  777. conn.retry_on_error is None
  778. or isinstance(error, tuple(conn.retry_on_error)) is False
  779. ):
  780. raise error
  781. await conn.connect()
  782. async def _execute(self, conn, command, *args, **kwargs):
  783. """
  784. Connect manually upon disconnection. If the Redis server is down,
  785. this will fail and raise a ConnectionError as desired.
  786. After reconnection, the ``on_connect`` callback should have been
  787. called by the # connection to resubscribe us to any channels and
  788. patterns we were previously listening to
  789. """
  790. return await conn.retry.call_with_retry(
  791. lambda: command(*args, **kwargs),
  792. lambda error: self._disconnect_raise_connect(conn, error),
  793. )
  794. async def parse_response(self, block: bool = True, timeout: float = 0):
  795. """Parse the response from a publish/subscribe command"""
  796. conn = self.connection
  797. if conn is None:
  798. raise RuntimeError(
  799. "pubsub connection not set: "
  800. "did you forget to call subscribe() or psubscribe()?"
  801. )
  802. await self.check_health()
  803. if not conn.is_connected:
  804. await conn.connect()
  805. read_timeout = None if block else timeout
  806. response = await self._execute(
  807. conn,
  808. conn.read_response,
  809. timeout=read_timeout,
  810. disconnect_on_error=False,
  811. push_request=True,
  812. )
  813. if conn.health_check_interval and response in self.health_check_response:
  814. # ignore the health check message as user might not expect it
  815. return None
  816. return response
  817. async def check_health(self):
  818. conn = self.connection
  819. if conn is None:
  820. raise RuntimeError(
  821. "pubsub connection not set: "
  822. "did you forget to call subscribe() or psubscribe()?"
  823. )
  824. if (
  825. conn.health_check_interval
  826. and asyncio.get_running_loop().time() > conn.next_health_check
  827. ):
  828. await conn.send_command(
  829. "PING", self.HEALTH_CHECK_MESSAGE, check_health=False
  830. )
  831. def _normalize_keys(self, data: _NormalizeKeysT) -> _NormalizeKeysT:
  832. """
  833. normalize channel/pattern names to be either bytes or strings
  834. based on whether responses are automatically decoded. this saves us
  835. from coercing the value for each message coming in.
  836. """
  837. encode = self.encoder.encode
  838. decode = self.encoder.decode
  839. return {decode(encode(k)): v for k, v in data.items()} # type: ignore[return-value] # noqa: E501
  840. async def psubscribe(self, *args: ChannelT, **kwargs: PubSubHandler):
  841. """
  842. Subscribe to channel patterns. Patterns supplied as keyword arguments
  843. expect a pattern name as the key and a callable as the value. A
  844. pattern's callable will be invoked automatically when a message is
  845. received on that pattern rather than producing a message via
  846. ``listen()``.
  847. """
  848. parsed_args = list_or_args((args[0],), args[1:]) if args else args
  849. new_patterns: Dict[ChannelT, PubSubHandler] = dict.fromkeys(parsed_args)
  850. # Mypy bug: https://github.com/python/mypy/issues/10970
  851. new_patterns.update(kwargs) # type: ignore[arg-type]
  852. ret_val = await self.execute_command("PSUBSCRIBE", *new_patterns.keys())
  853. # update the patterns dict AFTER we send the command. we don't want to
  854. # subscribe twice to these patterns, once for the command and again
  855. # for the reconnection.
  856. new_patterns = self._normalize_keys(new_patterns)
  857. self.patterns.update(new_patterns)
  858. self.pending_unsubscribe_patterns.difference_update(new_patterns)
  859. return ret_val
  860. def punsubscribe(self, *args: ChannelT) -> Awaitable:
  861. """
  862. Unsubscribe from the supplied patterns. If empty, unsubscribe from
  863. all patterns.
  864. """
  865. patterns: Iterable[ChannelT]
  866. if args:
  867. parsed_args = list_or_args((args[0],), args[1:])
  868. patterns = self._normalize_keys(dict.fromkeys(parsed_args)).keys()
  869. else:
  870. parsed_args = []
  871. patterns = self.patterns
  872. self.pending_unsubscribe_patterns.update(patterns)
  873. return self.execute_command("PUNSUBSCRIBE", *parsed_args)
  874. async def subscribe(self, *args: ChannelT, **kwargs: Callable):
  875. """
  876. Subscribe to channels. Channels supplied as keyword arguments expect
  877. a channel name as the key and a callable as the value. A channel's
  878. callable will be invoked automatically when a message is received on
  879. that channel rather than producing a message via ``listen()`` or
  880. ``get_message()``.
  881. """
  882. parsed_args = list_or_args((args[0],), args[1:]) if args else ()
  883. new_channels = dict.fromkeys(parsed_args)
  884. # Mypy bug: https://github.com/python/mypy/issues/10970
  885. new_channels.update(kwargs) # type: ignore[arg-type]
  886. ret_val = await self.execute_command("SUBSCRIBE", *new_channels.keys())
  887. # update the channels dict AFTER we send the command. we don't want to
  888. # subscribe twice to these channels, once for the command and again
  889. # for the reconnection.
  890. new_channels = self._normalize_keys(new_channels)
  891. self.channels.update(new_channels)
  892. self.pending_unsubscribe_channels.difference_update(new_channels)
  893. return ret_val
  894. def unsubscribe(self, *args) -> Awaitable:
  895. """
  896. Unsubscribe from the supplied channels. If empty, unsubscribe from
  897. all channels
  898. """
  899. if args:
  900. parsed_args = list_or_args(args[0], args[1:])
  901. channels = self._normalize_keys(dict.fromkeys(parsed_args))
  902. else:
  903. parsed_args = []
  904. channels = self.channels
  905. self.pending_unsubscribe_channels.update(channels)
  906. return self.execute_command("UNSUBSCRIBE", *parsed_args)
  907. async def listen(self) -> AsyncIterator:
  908. """Listen for messages on channels this client has been subscribed to"""
  909. while self.subscribed:
  910. response = await self.handle_message(await self.parse_response(block=True))
  911. if response is not None:
  912. yield response
  913. async def get_message(
  914. self, ignore_subscribe_messages: bool = False, timeout: Optional[float] = 0.0
  915. ):
  916. """
  917. Get the next message if one is available, otherwise None.
  918. If timeout is specified, the system will wait for `timeout` seconds
  919. before returning. Timeout should be specified as a floating point
  920. number or None to wait indefinitely.
  921. """
  922. response = await self.parse_response(block=(timeout is None), timeout=timeout)
  923. if response:
  924. return await self.handle_message(response, ignore_subscribe_messages)
  925. return None
  926. def ping(self, message=None) -> Awaitable:
  927. """
  928. Ping the Redis server
  929. """
  930. args = ["PING", message] if message is not None else ["PING"]
  931. return self.execute_command(*args)
  932. async def handle_message(self, response, ignore_subscribe_messages=False):
  933. """
  934. Parses a pub/sub message. If the channel or pattern was subscribed to
  935. with a message handler, the handler is invoked instead of a parsed
  936. message being returned.
  937. """
  938. if response is None:
  939. return None
  940. if isinstance(response, bytes):
  941. response = [b"pong", response] if response != b"PONG" else [b"pong", b""]
  942. message_type = str_if_bytes(response[0])
  943. if message_type == "pmessage":
  944. message = {
  945. "type": message_type,
  946. "pattern": response[1],
  947. "channel": response[2],
  948. "data": response[3],
  949. }
  950. elif message_type == "pong":
  951. message = {
  952. "type": message_type,
  953. "pattern": None,
  954. "channel": None,
  955. "data": response[1],
  956. }
  957. else:
  958. message = {
  959. "type": message_type,
  960. "pattern": None,
  961. "channel": response[1],
  962. "data": response[2],
  963. }
  964. # if this is an unsubscribe message, remove it from memory
  965. if message_type in self.UNSUBSCRIBE_MESSAGE_TYPES:
  966. if message_type == "punsubscribe":
  967. pattern = response[1]
  968. if pattern in self.pending_unsubscribe_patterns:
  969. self.pending_unsubscribe_patterns.remove(pattern)
  970. self.patterns.pop(pattern, None)
  971. else:
  972. channel = response[1]
  973. if channel in self.pending_unsubscribe_channels:
  974. self.pending_unsubscribe_channels.remove(channel)
  975. self.channels.pop(channel, None)
  976. if message_type in self.PUBLISH_MESSAGE_TYPES:
  977. # if there's a message handler, invoke it
  978. if message_type == "pmessage":
  979. handler = self.patterns.get(message["pattern"], None)
  980. else:
  981. handler = self.channels.get(message["channel"], None)
  982. if handler:
  983. if inspect.iscoroutinefunction(handler):
  984. await handler(message)
  985. else:
  986. handler(message)
  987. return None
  988. elif message_type != "pong":
  989. # this is a subscribe/unsubscribe message. ignore if we don't
  990. # want them
  991. if ignore_subscribe_messages or self.ignore_subscribe_messages:
  992. return None
  993. return message
  994. async def run(
  995. self,
  996. *,
  997. exception_handler: Optional["PSWorkerThreadExcHandlerT"] = None,
  998. poll_timeout: float = 1.0,
  999. ) -> None:
  1000. """Process pub/sub messages using registered callbacks.
  1001. This is the equivalent of :py:meth:`redis.PubSub.run_in_thread` in
  1002. redis-py, but it is a coroutine. To launch it as a separate task, use
  1003. ``asyncio.create_task``:
  1004. >>> task = asyncio.create_task(pubsub.run())
  1005. To shut it down, use asyncio cancellation:
  1006. >>> task.cancel()
  1007. >>> await task
  1008. """
  1009. for channel, handler in self.channels.items():
  1010. if handler is None:
  1011. raise PubSubError(f"Channel: '{channel}' has no handler registered")
  1012. for pattern, handler in self.patterns.items():
  1013. if handler is None:
  1014. raise PubSubError(f"Pattern: '{pattern}' has no handler registered")
  1015. await self.connect()
  1016. while True:
  1017. try:
  1018. await self.get_message(
  1019. ignore_subscribe_messages=True, timeout=poll_timeout
  1020. )
  1021. except asyncio.CancelledError:
  1022. raise
  1023. except BaseException as e:
  1024. if exception_handler is None:
  1025. raise
  1026. res = exception_handler(e, self)
  1027. if inspect.isawaitable(res):
  1028. await res
  1029. # Ensure that other tasks on the event loop get a chance to run
  1030. # if we didn't have to block for I/O anywhere.
  1031. await asyncio.sleep(0)
  1032. class PubsubWorkerExceptionHandler(Protocol):
  1033. def __call__(self, e: BaseException, pubsub: PubSub):
  1034. ...
  1035. class AsyncPubsubWorkerExceptionHandler(Protocol):
  1036. async def __call__(self, e: BaseException, pubsub: PubSub):
  1037. ...
  1038. PSWorkerThreadExcHandlerT = Union[
  1039. PubsubWorkerExceptionHandler, AsyncPubsubWorkerExceptionHandler
  1040. ]
  1041. CommandT = Tuple[Tuple[Union[str, bytes], ...], Mapping[str, Any]]
  1042. CommandStackT = List[CommandT]
  1043. class Pipeline(Redis): # lgtm [py/init-calls-subclass]
  1044. """
  1045. Pipelines provide a way to transmit multiple commands to the Redis server
  1046. in one transmission. This is convenient for batch processing, such as
  1047. saving all the values in a list to Redis.
  1048. All commands executed within a pipeline are wrapped with MULTI and EXEC
  1049. calls. This guarantees all commands executed in the pipeline will be
  1050. executed atomically.
  1051. Any command raising an exception does *not* halt the execution of
  1052. subsequent commands in the pipeline. Instead, the exception is caught
  1053. and its instance is placed into the response list returned by execute().
  1054. Code iterating over the response list should be able to deal with an
  1055. instance of an exception as a potential value. In general, these will be
  1056. ResponseError exceptions, such as those raised when issuing a command
  1057. on a key of a different datatype.
  1058. """
  1059. UNWATCH_COMMANDS = {"DISCARD", "EXEC", "UNWATCH"}
  1060. def __init__(
  1061. self,
  1062. connection_pool: ConnectionPool,
  1063. response_callbacks: MutableMapping[Union[str, bytes], ResponseCallbackT],
  1064. transaction: bool,
  1065. shard_hint: Optional[str],
  1066. ):
  1067. self.connection_pool = connection_pool
  1068. self.connection = None
  1069. self.response_callbacks = response_callbacks
  1070. self.is_transaction = transaction
  1071. self.shard_hint = shard_hint
  1072. self.watching = False
  1073. self.command_stack: CommandStackT = []
  1074. self.scripts: Set["Script"] = set()
  1075. self.explicit_transaction = False
  1076. async def __aenter__(self: _RedisT) -> _RedisT:
  1077. return self
  1078. async def __aexit__(self, exc_type, exc_value, traceback):
  1079. await self.reset()
  1080. def __await__(self):
  1081. return self._async_self().__await__()
  1082. _DEL_MESSAGE = "Unclosed Pipeline client"
  1083. def __len__(self):
  1084. return len(self.command_stack)
  1085. def __bool__(self):
  1086. """Pipeline instances should always evaluate to True"""
  1087. return True
  1088. async def _async_self(self):
  1089. return self
  1090. async def reset(self):
  1091. self.command_stack = []
  1092. self.scripts = set()
  1093. # make sure to reset the connection state in the event that we were
  1094. # watching something
  1095. if self.watching and self.connection:
  1096. try:
  1097. # call this manually since our unwatch or
  1098. # immediate_execute_command methods can call reset()
  1099. await self.connection.send_command("UNWATCH")
  1100. await self.connection.read_response()
  1101. except ConnectionError:
  1102. # disconnect will also remove any previous WATCHes
  1103. if self.connection:
  1104. await self.connection.disconnect()
  1105. # clean up the other instance attributes
  1106. self.watching = False
  1107. self.explicit_transaction = False
  1108. # we can safely return the connection to the pool here since we're
  1109. # sure we're no longer WATCHing anything
  1110. if self.connection:
  1111. await self.connection_pool.release(self.connection)
  1112. self.connection = None
  1113. async def aclose(self) -> None:
  1114. """Alias for reset(), a standard method name for cleanup"""
  1115. await self.reset()
  1116. def multi(self):
  1117. """
  1118. Start a transactional block of the pipeline after WATCH commands
  1119. are issued. End the transactional block with `execute`.
  1120. """
  1121. if self.explicit_transaction:
  1122. raise RedisError("Cannot issue nested calls to MULTI")
  1123. if self.command_stack:
  1124. raise RedisError(
  1125. "Commands without an initial WATCH have already been issued"
  1126. )
  1127. self.explicit_transaction = True
  1128. def execute_command(
  1129. self, *args, **kwargs
  1130. ) -> Union["Pipeline", Awaitable["Pipeline"]]:
  1131. if (self.watching or args[0] == "WATCH") and not self.explicit_transaction:
  1132. return self.immediate_execute_command(*args, **kwargs)
  1133. return self.pipeline_execute_command(*args, **kwargs)
  1134. async def _disconnect_reset_raise(self, conn, error):
  1135. """
  1136. Close the connection, reset watching state and
  1137. raise an exception if we were watching,
  1138. if retry_on_error is not set or the error is not one
  1139. of the specified error types.
  1140. """
  1141. await conn.disconnect()
  1142. # if we were already watching a variable, the watch is no longer
  1143. # valid since this connection has died. raise a WatchError, which
  1144. # indicates the user should retry this transaction.
  1145. if self.watching:
  1146. await self.aclose()
  1147. raise WatchError(
  1148. "A ConnectionError occurred on while watching one or more keys"
  1149. )
  1150. # if retry_on_error is not set or the error is not one
  1151. # of the specified error types, raise it
  1152. if (
  1153. conn.retry_on_error is None
  1154. or isinstance(error, tuple(conn.retry_on_error)) is False
  1155. ):
  1156. await self.aclose()
  1157. raise
  1158. async def immediate_execute_command(self, *args, **options):
  1159. """
  1160. Execute a command immediately, but don't auto-retry on a
  1161. ConnectionError if we're already WATCHing a variable. Used when
  1162. issuing WATCH or subsequent commands retrieving their values but before
  1163. MULTI is called.
  1164. """
  1165. command_name = args[0]
  1166. conn = self.connection
  1167. # if this is the first call, we need a connection
  1168. if not conn:
  1169. conn = await self.connection_pool.get_connection(
  1170. command_name, self.shard_hint
  1171. )
  1172. self.connection = conn
  1173. return await conn.retry.call_with_retry(
  1174. lambda: self._send_command_parse_response(
  1175. conn, command_name, *args, **options
  1176. ),
  1177. lambda error: self._disconnect_reset_raise(conn, error),
  1178. )
  1179. def pipeline_execute_command(self, *args, **options):
  1180. """
  1181. Stage a command to be executed when execute() is next called
  1182. Returns the current Pipeline object back so commands can be
  1183. chained together, such as:
  1184. pipe = pipe.set('foo', 'bar').incr('baz').decr('bang')
  1185. At some other point, you can then run: pipe.execute(),
  1186. which will execute all commands queued in the pipe.
  1187. """
  1188. self.command_stack.append((args, options))
  1189. return self
  1190. async def _execute_transaction( # noqa: C901
  1191. self, connection: Connection, commands: CommandStackT, raise_on_error
  1192. ):
  1193. pre: CommandT = (("MULTI",), {})
  1194. post: CommandT = (("EXEC",), {})
  1195. cmds = (pre, *commands, post)
  1196. all_cmds = connection.pack_commands(
  1197. args for args, options in cmds if EMPTY_RESPONSE not in options
  1198. )
  1199. await connection.send_packed_command(all_cmds)
  1200. errors = []
  1201. # parse off the response for MULTI
  1202. # NOTE: we need to handle ResponseErrors here and continue
  1203. # so that we read all the additional command messages from
  1204. # the socket
  1205. try:
  1206. await self.parse_response(connection, "_")
  1207. except ResponseError as err:
  1208. errors.append((0, err))
  1209. # and all the other commands
  1210. for i, command in enumerate(commands):
  1211. if EMPTY_RESPONSE in command[1]:
  1212. errors.append((i, command[1][EMPTY_RESPONSE]))
  1213. else:
  1214. try:
  1215. await self.parse_response(connection, "_")
  1216. except ResponseError as err:
  1217. self.annotate_exception(err, i + 1, command[0])
  1218. errors.append((i, err))
  1219. # parse the EXEC.
  1220. try:
  1221. response = await self.parse_response(connection, "_")
  1222. except ExecAbortError as err:
  1223. if errors:
  1224. raise errors[0][1] from err
  1225. raise
  1226. # EXEC clears any watched keys
  1227. self.watching = False
  1228. if response is None:
  1229. raise WatchError("Watched variable changed.") from None
  1230. # put any parse errors into the response
  1231. for i, e in errors:
  1232. response.insert(i, e)
  1233. if len(response) != len(commands):
  1234. if self.connection:
  1235. await self.connection.disconnect()
  1236. raise ResponseError(
  1237. "Wrong number of response items from pipeline execution"
  1238. ) from None
  1239. # find any errors in the response and raise if necessary
  1240. if raise_on_error:
  1241. self.raise_first_error(commands, response)
  1242. # We have to run response callbacks manually
  1243. data = []
  1244. for r, cmd in zip(response, commands):
  1245. if not isinstance(r, Exception):
  1246. args, options = cmd
  1247. command_name = args[0]
  1248. if command_name in self.response_callbacks:
  1249. r = self.response_callbacks[command_name](r, **options)
  1250. if inspect.isawaitable(r):
  1251. r = await r
  1252. data.append(r)
  1253. return data
  1254. async def _execute_pipeline(
  1255. self, connection: Connection, commands: CommandStackT, raise_on_error: bool
  1256. ):
  1257. # build up all commands into a single request to increase network perf
  1258. all_cmds = connection.pack_commands([args for args, _ in commands])
  1259. await connection.send_packed_command(all_cmds)
  1260. response = []
  1261. for args, options in commands:
  1262. try:
  1263. response.append(
  1264. await self.parse_response(connection, args[0], **options)
  1265. )
  1266. except ResponseError as e:
  1267. response.append(e)
  1268. if raise_on_error:
  1269. self.raise_first_error(commands, response)
  1270. return response
  1271. def raise_first_error(self, commands: CommandStackT, response: Iterable[Any]):
  1272. for i, r in enumerate(response):
  1273. if isinstance(r, ResponseError):
  1274. self.annotate_exception(r, i + 1, commands[i][0])
  1275. raise r
  1276. def annotate_exception(
  1277. self, exception: Exception, number: int, command: Iterable[object]
  1278. ) -> None:
  1279. cmd = " ".join(map(safe_str, command))
  1280. msg = f"Command # {number} ({cmd}) of pipeline caused error: {exception.args}"
  1281. exception.args = (msg,) + exception.args[1:]
  1282. async def parse_response(
  1283. self, connection: Connection, command_name: Union[str, bytes], **options
  1284. ):
  1285. result = await super().parse_response(connection, command_name, **options)
  1286. if command_name in self.UNWATCH_COMMANDS:
  1287. self.watching = False
  1288. elif command_name == "WATCH":
  1289. self.watching = True
  1290. return result
  1291. async def load_scripts(self):
  1292. # make sure all scripts that are about to be run on this pipeline exist
  1293. scripts = list(self.scripts)
  1294. immediate = self.immediate_execute_command
  1295. shas = [s.sha for s in scripts]
  1296. # we can't use the normal script_* methods because they would just
  1297. # get buffered in the pipeline.
  1298. exists = await immediate("SCRIPT EXISTS", *shas)
  1299. if not all(exists):
  1300. for s, exist in zip(scripts, exists):
  1301. if not exist:
  1302. s.sha = await immediate("SCRIPT LOAD", s.script)
  1303. async def _disconnect_raise_reset(self, conn: Connection, error: Exception):
  1304. """
  1305. Close the connection, raise an exception if we were watching,
  1306. and raise an exception if retry_on_error is not set or the
  1307. error is not one of the specified error types.
  1308. """
  1309. await conn.disconnect()
  1310. # if we were watching a variable, the watch is no longer valid
  1311. # since this connection has died. raise a WatchError, which
  1312. # indicates the user should retry this transaction.
  1313. if self.watching:
  1314. raise WatchError(
  1315. "A ConnectionError occurred on while watching one or more keys"
  1316. )
  1317. # if retry_on_error is not set or the error is not one
  1318. # of the specified error types, raise it
  1319. if (
  1320. conn.retry_on_error is None
  1321. or isinstance(error, tuple(conn.retry_on_error)) is False
  1322. ):
  1323. await self.reset()
  1324. raise
  1325. async def execute(self, raise_on_error: bool = True):
  1326. """Execute all the commands in the current pipeline"""
  1327. stack = self.command_stack
  1328. if not stack and not self.watching:
  1329. return []
  1330. if self.scripts:
  1331. await self.load_scripts()
  1332. if self.is_transaction or self.explicit_transaction:
  1333. execute = self._execute_transaction
  1334. else:
  1335. execute = self._execute_pipeline
  1336. conn = self.connection
  1337. if not conn:
  1338. conn = await self.connection_pool.get_connection("MULTI", self.shard_hint)
  1339. # assign to self.connection so reset() releases the connection
  1340. # back to the pool after we're done
  1341. self.connection = conn
  1342. conn = cast(Connection, conn)
  1343. try:
  1344. return await conn.retry.call_with_retry(
  1345. lambda: execute(conn, stack, raise_on_error),
  1346. lambda error: self._disconnect_raise_reset(conn, error),
  1347. )
  1348. finally:
  1349. await self.reset()
  1350. async def discard(self):
  1351. """Flushes all previously queued commands
  1352. See: https://redis.io/commands/DISCARD
  1353. """
  1354. await self.execute_command("DISCARD")
  1355. async def watch(self, *names: KeyT):
  1356. """Watches the values at keys ``names``"""
  1357. if self.explicit_transaction:
  1358. raise RedisError("Cannot issue a WATCH after a MULTI")
  1359. return await self.execute_command("WATCH", *names)
  1360. async def unwatch(self):
  1361. """Unwatches all previously specified keys"""
  1362. return self.watching and await self.execute_command("UNWATCH") or True