use threading for updates checkign
This commit is contained in:
parent
29306b42f5
commit
22bce5b147
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
|
from concurrent.futures.thread import ThreadPoolExecutor
|
||||||
from multiprocessing.pool import ThreadPool
|
from multiprocessing.pool import ThreadPool
|
||||||
|
|
||||||
import sentry_sdk
|
import sentry_sdk
|
||||||
@ -21,6 +22,21 @@ class Command(BaseCommand):
|
|||||||
logging.exception('Exception while processing update', exc_info=ctx.error)
|
logging.exception('Exception while processing update', exc_info=ctx.error)
|
||||||
|
|
||||||
dispatchers = []
|
dispatchers = []
|
||||||
|
|
||||||
|
def check_updates(dispatcher):
|
||||||
|
try:
|
||||||
|
updates = dispatcher.bot.get_updates(dispatcher.last_update_id)
|
||||||
|
except TimedOut:
|
||||||
|
return
|
||||||
|
except TelegramError as e:
|
||||||
|
sentry_sdk.capture_exception(e)
|
||||||
|
traceback.print_exc()
|
||||||
|
updates = []
|
||||||
|
|
||||||
|
for update in updates:
|
||||||
|
pool.apply_async(dispatcher.process_update, (update,))
|
||||||
|
dispatcher.last_update_id = update.update_id + 1
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
if not dispatchers or cache.get('bots_reset'):
|
if not dispatchers or cache.get('bots_reset'):
|
||||||
@ -35,19 +51,9 @@ class Command(BaseCommand):
|
|||||||
pass
|
pass
|
||||||
cache.delete('bots_reset')
|
cache.delete('bots_reset')
|
||||||
|
|
||||||
for dispatcher in dispatchers:
|
with ThreadPoolExecutor() as executor:
|
||||||
try:
|
for dispatcher in dispatchers:
|
||||||
updates = dispatcher.bot.get_updates(dispatcher.last_update_id)
|
executor.submit(check_updates, dispatcher)
|
||||||
except TimedOut:
|
|
||||||
continue
|
|
||||||
except TelegramError as e:
|
|
||||||
sentry_sdk.capture_exception(e)
|
|
||||||
traceback.print_exc()
|
|
||||||
updates = []
|
|
||||||
|
|
||||||
for update in updates:
|
|
||||||
pool.apply_async(dispatcher.process_update, (update,))
|
|
||||||
dispatcher.last_update_id = update.update_id + 1
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pool.terminate()
|
pool.terminate()
|
||||||
pool.join()
|
pool.join()
|
||||||
|
Loading…
Reference in New Issue
Block a user