@@ -17,9 +17,9 @@ def __init__(self, connection_settings = {}, resume_stream = False, blocking = F
17
17
blocking: Read on stream is blocking
18
18
only_events: Array of allowed events
19
19
'''
20
- connection_settings [ 'charset' ] = 'utf8'
21
- self ._stream_connection = pymysql . connect ( ** connection_settings )
22
- ctl_connection_settings = copy .copy (connection_settings )
20
+ self . __connection_settings = connection_settings
21
+ self .__connection_settings [ 'charset' ] = 'utf8'
22
+ ctl_connection_settings = copy .copy (self . __connection_settings )
23
23
ctl_connection_settings ['db' ] = 'information_schema'
24
24
ctl_connection_settings ['cursorclass' ] = pymysql .cursors .DictCursor
25
25
self .__ctl_connection = pymysql .connect (** ctl_connection_settings )
@@ -34,10 +34,13 @@ def __init__(self, connection_settings = {}, resume_stream = False, blocking = F
34
34
self .table_map = {}
35
35
36
36
def close (self ):
37
- self ._stream_connection .close ()
37
+ if self .__connected :
38
+ self ._stream_connection .close ()
39
+ self .__connected = False
38
40
self .__ctl_connection .close ()
39
41
40
42
def __connect_to_stream (self ):
43
+ self ._stream_connection = pymysql .connect (** self .__connection_settings )
41
44
cur = self ._stream_connection .cursor ()
42
45
cur .execute ("SHOW MASTER STATUS" )
43
46
(log_file , log_pos ) = cur .fetchone ()[:2 ]
@@ -68,17 +71,24 @@ def __connect_to_stream(self):
68
71
self .__connected = True
69
72
70
73
def fetchone (self ):
71
- if self .__connected == False :
72
- self .__connect_to_stream ()
73
74
while True :
74
- pkt = self ._stream_connection .read_packet ()
75
+ if self .__connected == False :
76
+ self .__connect_to_stream ()
77
+ pkt = None
78
+ try :
79
+ pkt = self ._stream_connection .read_packet ()
80
+ except pymysql .OperationalError as (code , message ):
81
+ if code == 2013 : #2013: Connection Lost
82
+ self .__connected = False
83
+ continue
75
84
if not pkt .is_ok_packet ():
76
85
return None
77
86
binlog_event = BinLogPacketWrapper (pkt , self .table_map , self .__ctl_connection )
78
87
if binlog_event .event_type == TABLE_MAP_EVENT :
79
88
self .table_map [binlog_event .event .table_id ] = binlog_event .event
80
89
if self .__filter_event (binlog_event .event ):
81
90
continue
91
+ self .__log_pos = binlog_event .log_pos
82
92
return binlog_event .event
83
93
84
94
def __filter_event (self , event ):
0 commit comments