3
3
4
4
from collections import deque
5
5
import warnings
6
+ import logging
6
7
7
8
from tornado .ioloop import IOLoop
8
9
from tornado .gen import coroutine , Return
12
13
from tornado_mysql .connections import Connection
13
14
14
15
15
- DEBUG = False
16
-
17
-
18
- def _debug (* msg ):
19
- if DEBUG :
20
- print (* msg )
16
+ log = logging .getLogger ("tornado_mysql.pools" )
21
17
22
18
23
19
class Pool (object ):
@@ -67,15 +63,15 @@ def _get_conn(self):
67
63
if now - conn .connected_time > self .max_recycle_sec :
68
64
self ._close_async (conn )
69
65
continue
70
- _debug ("Reusing connection from pool:" , self .stat ())
66
+ log . debug ("Reusing connection from pool: %s " , self .stat ())
71
67
fut = Future ()
72
68
fut .set_result (conn )
73
69
return fut
74
70
75
71
# Open new connection
76
72
if self .max_open == 0 or self ._opened_conns < self .max_open :
77
73
self ._opened_conns += 1
78
- _debug ("Creating new connection:" , self .stat ())
74
+ log . debug ("Creating new connection: %s " , self .stat ())
79
75
return connect (** self .connect_kwargs )
80
76
81
77
# Wait to other connection is released.
@@ -89,10 +85,10 @@ def _put_conn(self, conn):
89
85
if self ._waitings :
90
86
fut = self ._waitings .popleft ()
91
87
fut .set_result (conn )
92
- _debug ("Passing returned connection to waiter:" , self .stat ())
88
+ log . debug ("Passing returned connection to waiter: %s " , self .stat ())
93
89
else :
94
90
self ._free_conn .append (conn )
95
- _debug ("Add conn to free pool:" , self .stat ())
91
+ log . debug ("Add conn to free pool: %s " , self .stat ())
96
92
else :
97
93
self ._close_async (conn )
98
94
@@ -111,7 +107,7 @@ def _after_close(self, fut=None):
111
107
self .io_loop .add_future (cf , callback = lambda f : fut .set_result (conn ))
112
108
else :
113
109
self ._opened_conns -= 1
114
- _debug ("Connection closed:" , self .stat ())
110
+ log . debug ("Connection closed: %s " , self .stat ())
115
111
116
112
@coroutine
117
113
def execute (self , query , params = None ):
@@ -194,4 +190,5 @@ def rollback(self):
194
190
def __del__ (self ):
195
191
if self ._pool is not None :
196
192
warnings .warn ("Transaction has not committed or rollbacked." )
193
+ log .warn ("Transaction has not committed or rollbacked." )
197
194
self ._pool ._close_conn (self ._conn )
0 commit comments