3636ASYNC_QUEUE = Queue ()
3737ASYNC_THREADS = set ()
3838""":type: set[Thread]"""
39- ASYNC_LOCK = Lock ()
39+ ASYNC_LOCK = Lock () # guards ASYNC_THREADS
4040DEFAULT_GROUP = 0
4141
4242
@@ -48,16 +48,17 @@ def _pooled():
4848 try :
4949 func , args , kwargs = ASYNC_QUEUE .get ()
5050
51+ # If unpacking fails, the thread pool is being closed from Updater._join_async_threads
5152 except TypeError :
52- logging .debug ("Closing run_async thread %s/%d" %
53- (current_thread ().getName (), len (ASYNC_THREADS )))
53+ logging .getLogger ( __name__ ). debug ("Closing run_async thread %s/%d" %
54+ (current_thread ().getName (), len (ASYNC_THREADS )))
5455 break
5556
5657 try :
5758 func (* args , ** kwargs )
5859
5960 except :
60- logging .exception ("Async function raised exception" )
61+ logging .getLogger ( __name__ ). exception ("run_async function raised exception" )
6162
6263
6364def run_async (func ):
@@ -110,17 +111,18 @@ def __init__(self, bot, update_queue, workers=4, exception_event=None):
110111 self .__stop_event = Event ()
111112 self .__exception_event = exception_event or Event ()
112113
113- if not len (ASYNC_THREADS ):
114- if request .is_con_pool_initialized ():
115- raise RuntimeError ('Connection Pool already initialized' )
114+ with ASYNC_LOCK :
115+ if not ASYNC_THREADS :
116+ if request .is_con_pool_initialized ():
117+ raise RuntimeError ('Connection Pool already initialized' )
116118
117- request .CON_POOL_SIZE = workers + 3
118- for i in range (workers ):
119- thread = Thread (target = _pooled , name = str (i ))
120- ASYNC_THREADS .add (thread )
121- thread .start ()
122- else :
123- self .logger .debug ('Thread pool already initialized, skipping.' )
119+ request .CON_POOL_SIZE = workers + 3
120+ for i in range (workers ):
121+ thread = Thread (target = _pooled , name = str (i ))
122+ ASYNC_THREADS .add (thread )
123+ thread .start ()
124+ else :
125+ self .logger .debug ('Thread pool already initialized, skipping.' )
124126
125127 def start (self ):
126128 """
@@ -140,7 +142,7 @@ def start(self):
140142 self .running = True
141143 self .logger .debug ('Dispatcher started' )
142144
143- while True :
145+ while 1 :
144146 try :
145147 # Pop update from update queue.
146148 update = self .update_queue .get (True , 1 )
@@ -154,7 +156,7 @@ def start(self):
154156 continue
155157
156158 self .logger .debug ('Processing Update: %s' % update )
157- self .processUpdate (update )
159+ self .process_update (update )
158160
159161 self .running = False
160162 self .logger .debug ('Dispatcher thread stopped' )
@@ -169,7 +171,7 @@ def stop(self):
169171 sleep (0.1 )
170172 self .__stop_event .clear ()
171173
172- def processUpdate (self , update ):
174+ def process_update (self , update ):
173175 """
174176 Processes a single update.
175177
@@ -179,7 +181,7 @@ def processUpdate(self, update):
179181
180182 # An error happened while polling
181183 if isinstance (update , TelegramError ):
182- self .dispatchError (None , update )
184+ self .dispatch_error (None , update )
183185
184186 else :
185187 for group in self .groups :
@@ -194,7 +196,7 @@ def processUpdate(self, update):
194196 'Update.' )
195197
196198 try :
197- self .dispatchError (update , te )
199+ self .dispatch_error (update , te )
198200 except Exception :
199201 self .logger .exception ('An uncaught error was raised while '
200202 'handling the error' )
@@ -280,7 +282,7 @@ def remove_error_handler(self, callback):
280282 if callback in self .error_handlers :
281283 self .error_handlers .remove (callback )
282284
283- def dispatchError (self , update , error ):
285+ def dispatch_error (self , update , error ):
284286 """
285287 Dispatches an error.
286288
0 commit comments