@@ -153,14 +153,19 @@ def __init__(self, write_func, send_lock, data, seq_no, timeout=2, callback=None
153153 self .seq_no = seq_no
154154 self .timeout = timeout
155155 self .callback = callback
156- self .ack = Event ()
156+
157+ self .stop_sender = Event ()
158+ self .stop_timeout = Event ()
159+ self .next_timeout = 0
157160
158161 def run (self ):
159- with self .send_lock :
160- self .__send_data ()
162+ while not self .stop_sender .isSet ():
163+ self .stop_timeout .wait (max (0 , self .next_timeout - time ()))
164+ self .stop_timeout .clear ()
165+
166+ if not self .stop_sender .isSet ():
167+ self .next_timeout = time () + self .timeout
161168
162- while not self .ack .isSet ():
163- if time () >= self .next_timeout :
164169 with self .send_lock :
165170 self .__send_data ()
166171
@@ -169,7 +174,8 @@ def join(self, timeout=None):
169174 Stop the current thread.
170175 """
171176
172- self .ack .set ()
177+ self .stop_sender .set ()
178+ self .stop_timeout .set ()
173179 super ().join (timeout )
174180
175181 def ack_received (self ):
@@ -190,8 +196,7 @@ def nack_received(self):
190196 frame.
191197 """
192198
193- with self .send_lock :
194- self .__send_data ()
199+ self .stop_timeout .set ()
195200
196201 def __send_data (self ):
197202 """
@@ -202,7 +207,6 @@ def __send_data(self):
202207 self .callback (self .data )
203208
204209 self .write (frame_data (self .data , FRAME_DATA , self .seq_no ))
205- self .next_timeout = time () + self .timeout
206210
207211 class Receiver (Thread ):
208212 """
@@ -217,6 +221,7 @@ def __init__(self, read_func, write_func, send_lock, senders_list, frames_receiv
217221 self .senders = senders_list
218222 self .frames_received = frames_received
219223 self .callback = callback
224+
220225 self .stop_receiver = Event ()
221226
222227 def run (self ):
0 commit comments