Skip to content

Commit 46657af

Browse files
wronglinktsnoam
authored andcommitted
Start additional threads only when necessary (python-telegram-bot#415)
* Start all additional threads only when necessary. * Deprecate prevent_autostart in the c'tor of JobQueue.
1 parent 9d0e038 commit 46657af

6 files changed

Lines changed: 22 additions & 15 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec
2121
- `jlmadurga <https://github.com/jlmadurga>`_
2222
- `Li-aung Yip <https://github.com/LiaungYip>`_
2323
- `macrojames <https://github.com/macrojames>`_
24+
- `Michael Elovskikh <https://github.com/wronglink>`_
2425
- `naveenvhegde <https://github.com/naveenvhegde>`_
2526
- `njittam <https://github.com/njittam>`_
2627
- `Noam Meltzer <https://github.com/tsnoam>`_

telegram/ext/dispatcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def __init__(self, bot, update_queue, workers=4, exception_event=None, job_queue
8484
self.bot = bot
8585
self.update_queue = update_queue
8686
self.job_queue = job_queue
87+
self.workers = workers
8788

8889
self.handlers = {}
8990
""":type: dict[int, list[Handler]"""
@@ -105,8 +106,6 @@ def __init__(self, bot, update_queue, workers=4, exception_event=None, job_queue
105106
else:
106107
self._set_singleton(None)
107108

108-
self._init_async_threads(uuid4(), workers)
109-
110109
@classmethod
111110
def _reset_singleton(cls):
112111
# NOTE: This method was added mainly for test_updater benefit and specifically pypy. Never
@@ -193,6 +192,7 @@ def start(self):
193192
self.logger.error(msg)
194193
raise TelegramError(msg)
195194

195+
self._init_async_threads(uuid4(), self.workers)
196196
self.running = True
197197
self.logger.debug('Dispatcher started')
198198

telegram/ext/jobqueue.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import logging
2222
import time
23+
import warnings
2324
from threading import Thread, Lock, Event
2425
from queue import PriorityQueue, Empty
2526

@@ -30,15 +31,19 @@ class JobQueue(object):
3031
Attributes:
3132
queue (PriorityQueue):
3233
bot (Bot):
33-
prevent_autostart (Optional[bool]): If ``True``, the job queue will not be started
34-
automatically. Defaults to ``False``
3534
3635
Args:
3736
bot (Bot): The bot instance that should be passed to the jobs
3837
38+
Deprecated: 5.2
39+
prevent_autostart (Optional[bool]): Thread does not start during initialisation.
40+
Use `start` method instead.
3941
"""
4042

41-
def __init__(self, bot, prevent_autostart=False):
43+
def __init__(self, bot, prevent_autostart=None):
44+
if prevent_autostart is not None:
45+
warnings.warn("prevent_autostart is being deprecated, use `start` method instead.")
46+
4247
self.queue = PriorityQueue()
4348
self.bot = bot
4449
self.logger = logging.getLogger(self.__class__.__name__)
@@ -51,12 +56,8 @@ def __init__(self, bot, prevent_autostart=False):
5156
""":type: float"""
5257
self._running = False
5358

54-
if not prevent_autostart:
55-
self.logger.debug('Auto-starting %s', self.__class__.__name__)
56-
self.start()
57-
5859
def put(self, job, next_t=None):
59-
"""Queue a new job. If the JobQueue is not running, it will be started.
60+
"""Queue a new job.
6061
6162
Args:
6263
job (Job): The ``Job`` instance representing the new job

telegram/ext/updater.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def start_polling(self,
157157
self.running = True
158158

159159
# Create & start threads
160+
self.job_queue.start()
160161
self._init_thread(self.dispatcher.start, "dispatcher")
161162
self._init_thread(self._start_polling, "updater", poll_interval, timeout,
162163
network_delay, bootstrap_retries, clean)
@@ -208,6 +209,7 @@ def start_webhook(self,
208209
self.running = True
209210

210211
# Create & start threads
212+
self.job_queue.start()
211213
self._init_thread(self.dispatcher.start, "dispatcher"),
212214
self._init_thread(self._start_webhook, "updater", listen, port, url_path, cert,
213215
key, bootstrap_retries, clean, webhook_url)

tests/test_jobqueue.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class JobQueueTest(BaseTest, unittest.TestCase):
5151

5252
def setUp(self):
5353
self.jq = JobQueue(MockBot('jobqueue_test'))
54+
self.jq.start()
5455
self.result = 0
5556

5657
def tearDown(self):
@@ -143,7 +144,6 @@ def test_longer_first(self):
143144
def test_error(self):
144145
self.jq.put(Job(self.job2, 0.1))
145146
self.jq.put(Job(self.job1, 0.2))
146-
self.jq.start()
147147
sleep(0.5)
148148
self.assertEqual(2, self.result)
149149

@@ -158,6 +158,7 @@ def test_jobs_tuple(self):
158158

159159
def test_inUpdater(self):
160160
u = Updater(bot="MockBot")
161+
u.job_queue.start()
161162
try:
162163
u.job_queue.put(Job(self.job1, 0.5))
163164
sleep(0.75)

tests/test_updater.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,12 @@ def get_dispatcher_name(q):
427427
q.put(current_thread().name)
428428
sleep(1.2)
429429

430-
d1 = Dispatcher(MockBot('disp1'), Queue(), workers=1)
431-
d2 = Dispatcher(MockBot('disp2'), Queue(), workers=1)
430+
d1 = Dispatcher(MockBot('disp1'), Queue())
431+
d2 = Dispatcher(MockBot('disp2'), Queue())
432432
q1 = Queue()
433433
q2 = Queue()
434+
d1._init_async_threads('test_1', workers=1)
435+
d2._init_async_threads('test_2', workers=1)
434436

435437
try:
436438
d1.run_async(get_dispatcher_name, q1)
@@ -622,9 +624,9 @@ def test_webhook_no_ssl(self):
622624

623625
def test_start_dispatcher_twice(self):
624626
self._setup_updater('', messages=0)
625-
d = self.updater.dispatcher
626627
self.updater.start_polling(0.1)
627-
d.start()
628+
sleep(0.5)
629+
self.updater.dispatcher.start()
628630

629631
def test_bootstrap_retries_success(self):
630632
retries = 3

0 commit comments

Comments
 (0)