Skip to content

Commit 7e15cf8

Browse files
committed
Add waiter to MySQLdb.Connection.
1 parent 5ba6080 commit 7e15cf8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

MySQLdb/connections.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class Connection(_mysql.connection):
6363
"""MySQL Database Connection Object"""
6464

6565
default_cursor = cursors.Cursor
66+
waiter = None
6667

6768
def __init__(self, *args, **kwargs):
6869
"""
69-
7070
Create a connection to the database. It is strongly recommended
7171
that you only use keyword parameters. Consult the MySQL C API
7272
documentation for more information.
@@ -150,9 +150,13 @@ class object, used to create cursors (keyword only)
150150
If True, autocommit is enabled.
151151
If None, autocommit isn't set and server default is used.
152152
153+
waiter
154+
Callable accepts fd as an argument. It is called after sending
155+
query and before reading response.
156+
This is useful when using with greenlet and async io.
157+
153158
There are a number of undocumented, non-standard methods. See the
154159
documentation for the MySQL C API for some hints on what they do.
155-
156160
"""
157161
from MySQLdb.constants import CLIENT, FIELD_TYPE
158162
from MySQLdb.converters import conversions
@@ -195,6 +199,7 @@ class object, used to create cursors (keyword only)
195199

196200
# PEP-249 requires autocommit to be initially off
197201
autocommit = kwargs2.pop('autocommit', False)
202+
self.waiter = kwargs2.pop('waiter', None)
198203

199204
super(Connection, self).__init__(*args, **kwargs2)
200205
self.cursorclass = cursorclass
@@ -266,6 +271,14 @@ def cursor(self, cursorclass=None):
266271
"""
267272
return (cursorclass or self.cursorclass)(self)
268273

274+
def query(self, query):
275+
if self.waiter is not None:
276+
self.send_query(query)
277+
self.waiter(self.fileno())
278+
self.read_query_result()
279+
else:
280+
_mysql.connection.query(self, query)
281+
269282
def __enter__(self):
270283
if self.get_autocommit():
271284
self.query("BEGIN")

0 commit comments

Comments
 (0)