.. rubric:: Methods you can use to do something cool
- .. automethod:: cancel
.. automethod:: cancel_safe
+ .. warning::
+
+ This method shouldn't be used as a `~signal.signal` handler.
+ Please use `cancel()` instead.
+
+ .. versionadded:: 3.2
+
+ .. automethod:: cancel
+
+ .. warning::
+
+ The `!cancel()` method has a few shortcomings:
+
+ - it is blocking even on async connections,
+ - it `might use an insecure connection`__ even if the original
+ connection was secure.
+
+ Therefore you should use the `cancel_safe()` method whenever
+ possible.
+
+ .. __: https://www.postgresql.org/docs/devel/libpq-cancel.html
+ #LIBPQ-CANCEL-DEPRECATED
+
+ .. note::
+
+ Unlike `cancel_safe()`, it is safe to call this method as a
+ `~signal.signal` handler.
+
.. automethod:: notifies
Notifies are received after using :sql:`LISTEN` in a connection, when
async with conn.transaction() as tx:
...
+ .. automethod:: cancel_safe
+
+ .. versionadded:: 3.2
+
.. automethod:: notifies
.. versionchanged:: 3.2
def cancel_safe(self) -> None:
"""Cancel the current operation on the connection.
- This is a non-blocking version of `cancel()` which leverages a more
- secure and improved cancellation feature of the libpq (available from
- version 17).
+ This is a non-blocking version of `~Connection.cancel()` which
+ leverages a more secure and improved cancellation feature of the libpq,
+ which is only available from version 17.
- In contrast with `cancel()`, it is not appropriate for use in a signal
- handler.
-
- If the underlying libpq is older than version 17, fall back to
- `cancel()`.
+ If the underlying libpq is older than version 17, the method will fall
+ back to using the same implementation of `!cancel()`.
"""
if self._should_cancel():
try:
async def cancel_safe(self) -> None:
"""Cancel the current operation on the connection.
- This is a non-blocking version of `cancel()` which leverages a more
- secure and improved cancellation feature of the libpq (available from
- version 17).
+ This is a non-blocking version of `~Connection.cancel()` which
+ leverages a more secure and improved cancellation feature of the libpq,
+ which is only available from version 17.
- In contrast with `cancel()`, it is not appropriate for use in a signal
- handler.
-
- If the underlying libpq is older than version 17, fall back to
- `cancel()`.
+ If the underlying libpq is older than version 17, the method will fall
+ back to using the same implementation of `!cancel()`.
"""
if self._should_cancel():
try: