36
36
ASYNC_QUEUE = Queue ()
37
37
ASYNC_THREADS = set ()
38
38
""":type: set[Thread]"""
39
- ASYNC_LOCK = Lock ()
39
+ ASYNC_LOCK = Lock () # guards ASYNC_THREADS
40
40
DEFAULT_GROUP = 0
41
41
42
42
@@ -48,16 +48,17 @@ def _pooled():
48
48
try :
49
49
func , args , kwargs = ASYNC_QUEUE .get ()
50
50
51
+ # If unpacking fails, the thread pool is being closed from Updater._join_async_threads
51
52
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 )))
54
55
break
55
56
56
57
try :
57
58
func (* args , ** kwargs )
58
59
59
60
except :
60
- logging .exception ("Async function raised exception" )
61
+ logging .getLogger ( __name__ ). exception ("run_async function raised exception" )
61
62
62
63
63
64
def run_async (func ):
@@ -110,17 +111,18 @@ def __init__(self, bot, update_queue, workers=4, exception_event=None):
110
111
self .__stop_event = Event ()
111
112
self .__exception_event = exception_event or Event ()
112
113
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' )
116
118
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.' )
124
126
125
127
def start (self ):
126
128
"""
@@ -140,7 +142,7 @@ def start(self):
140
142
self .running = True
141
143
self .logger .debug ('Dispatcher started' )
142
144
143
- while True :
145
+ while 1 :
144
146
try :
145
147
# Pop update from update queue.
146
148
update = self .update_queue .get (True , 1 )
@@ -154,7 +156,7 @@ def start(self):
154
156
continue
155
157
156
158
self .logger .debug ('Processing Update: %s' % update )
157
- self .processUpdate (update )
159
+ self .process_update (update )
158
160
159
161
self .running = False
160
162
self .logger .debug ('Dispatcher thread stopped' )
@@ -169,7 +171,7 @@ def stop(self):
169
171
sleep (0.1 )
170
172
self .__stop_event .clear ()
171
173
172
- def processUpdate (self , update ):
174
+ def process_update (self , update ):
173
175
"""
174
176
Processes a single update.
175
177
@@ -179,7 +181,7 @@ def processUpdate(self, update):
179
181
180
182
# An error happened while polling
181
183
if isinstance (update , TelegramError ):
182
- self .dispatchError (None , update )
184
+ self .dispatch_error (None , update )
183
185
184
186
else :
185
187
for group in self .groups :
@@ -194,7 +196,7 @@ def processUpdate(self, update):
194
196
'Update.' )
195
197
196
198
try :
197
- self .dispatchError (update , te )
199
+ self .dispatch_error (update , te )
198
200
except Exception :
199
201
self .logger .exception ('An uncaught error was raised while '
200
202
'handling the error' )
@@ -280,7 +282,7 @@ def remove_error_handler(self, callback):
280
282
if callback in self .error_handlers :
281
283
self .error_handlers .remove (callback )
282
284
283
- def dispatchError (self , update , error ):
285
+ def dispatch_error (self , update , error ):
284
286
"""
285
287
Dispatches an error.
286
288
0 commit comments