8000 Adds a new option for adding a timeout for ping by smarden1 · Pull Request #1170 · PyMySQL/PyMySQL · GitHub
[go: up one dir, main page]

Skip to content

Adds a new option for adding a timeout for ping #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pymysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Connection:
(if no authenticate method) for returning a string from the user. (experimental)
:param server_public_key: SHA256 authentication plugin public key value. (default: None)
:param binary_prefix: Add _binary prefix on bytes and bytearray. (default: False)
:param timeout_on_ping: Set to true to re-use the connect_timeout for ping (default: False)
:param compress: Not supported.
:param named_pipe: Not supported.
:param db: **DEPRECATED** Alias for database.
Expand Down Expand Up @@ -204,6 +205,7 @@ def __init__(
ssl_key_password=None,
ssl_verify_cert=None,
ssl_verify_identity=None,
timeout_on_ping=False,
compress=None, # not supported
named_pipe=None, # not supported
passwd=None, # deprecated
Expand Down Expand Up @@ -311,6 +313,7 @@ def _config(key, arg):
if write_timeout is not None and write_timeout <= 0:
raise ValueError("write_timeout should be > 0")
self._write_timeout = write_timeout
self._timeout_on_ping = timeout_on_ping

self.charset = charset or DEFAULT_CHARSET
self.collation = collation
Expand Down Expand Up @@ -590,7 +593,15 @@ def ping(self, reconnect=True):
reconnect = False
else:
raise err.Error("Already closed")

read_timeout = self._read_timeout
write_timeout = self._write_timeout

try:
if self._timeout_on_ping:
self._read_timeout = self.connect_timeout
self._write_timeout = self.connect_timeout

self._execute_command(COMMAND.COM_PING, "")
self._read_ok_packet()
except Exception:
Expand All @@ -599,6 +610,10 @@ def ping(self, reconnect=True):
self.ping(False)
else:
raise
finally:
if self._timeout_on_ping:
self._read_timeout = read_timeout
self._write_timeout = write_timeout

def set_charset(self, charset):
"""Deprecated. Use set_character_set() instead."""
Expand Down
0