Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `jlmadurga <https://github.com/jlmadurga>`_
- `Li-aung Yip <https://github.com/LiaungYip>`_
- `macrojames <https://github.com/macrojames>`_
- `Michael Elovskikh <https://github.com/wronglink>`_
- `naveenvhegde <https://github.com/naveenvhegde>`_
- `njittam <https://github.com/njittam>`_
- `Noam Meltzer <https://github.com/tsnoam>`_
Expand Down
4 changes: 2 additions & 2 deletions telegram/ext/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, bot, update_queue, workers=4, exception_event=None, job_queue
self.bot = bot
self.update_queue = update_queue
self.job_queue = job_queue
self.workers = workers

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

self._init_async_threads(uuid4(), workers)

@classmethod
def _reset_singleton(cls):
# NOTE: This method was added mainly for test_updater benefit and specifically pypy. Never
Expand Down Expand Up @@ -193,6 +192,7 @@ def start(self):
self.logger.error(msg)
raise TelegramError(msg)

self._init_async_threads(uuid4(), self.workers)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree with this change, but I'd like @tsnoam to give it a look as well.

self.running = True
self.logger.debug('Dispatcher started')

Expand Down
17 changes: 9 additions & 8 deletions telegram/ext/jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import logging
import time
import warnings
from threading import Thread, Lock, Event
from queue import PriorityQueue, Empty

Expand All @@ -30,15 +31,19 @@ class JobQueue(object):
Attributes:
queue (PriorityQueue):
bot (Bot):
prevent_autostart (Optional[bool]): If ``True``, the job queue will not be started
automatically. Defaults to ``False``

Args:
bot (Bot): The bot instance that should be passed to the jobs

Deprecated: 5.2
prevent_autostart (Optional[bool]): Thread does not start during initialisation.
Use `start` method instead.
"""

def __init__(self, bot, prevent_autostart=False):
def __init__(self, bot, prevent_autostart=None):
if prevent_autostart is not None:
warnings.warn("prevent_autostart is being deprecated, use `start` method instead.")

self.queue = PriorityQueue()
self.bot = bot
self.logger = logging.getLogger(self.__class__.__name__)
Expand All @@ -51,12 +56,8 @@ def __init__(self, bot, prevent_autostart=False):
""":type: float"""
self._running = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

if not prevent_autostart:
self.logger.debug('Auto-starting %s', self.__class__.__name__)
self.start()

def put(self, job, next_t=None):
"""Queue a new job. If the JobQueue is not running, it will be started.
"""Queue a new job.

Args:
job (Job): The ``Job`` instance representing the new job
Expand Down
2 changes: 2 additions & 0 deletions telegram/ext/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def start_polling(self,
self.running = True

# Create & start threads
self.job_queue.start()
self._init_thread(self.dispatcher.start, "dispatcher")
self._init_thread(self._start_polling, "updater", poll_interval, timeout,
network_delay, bootstrap_retries, clean)
Expand Down Expand Up @@ -208,6 +209,7 @@ def start_webhook(self,
self.running = True

# Create & start threads
self.job_queue.start()
self._init_thread(self.dispatcher.start, "dispatcher"),
self._init_thread(self._start_webhook, "updater", listen, port, url_path, cert,
key, bootstrap_retries, clean, webhook_url)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class JobQueueTest(BaseTest, unittest.TestCase):

def setUp(self):
self.jq = JobQueue(MockBot('jobqueue_test'))
self.jq.start()
self.result = 0

def tearDown(self):
Expand Down Expand Up @@ -143,7 +144,6 @@ def test_longer_first(self):
def test_error(self):
self.jq.put(Job(self.job2, 0.1))
self.jq.put(Job(self.job1, 0.2))
self.jq.start()
sleep(0.5)
self.assertEqual(2, self.result)

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

def test_inUpdater(self):
u = Updater(bot="MockBot")
u.job_queue.start()
try:
u.job_queue.put(Job(self.job1, 0.5))
sleep(0.75)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,12 @@ def get_dispatcher_name(q):
q.put(current_thread().name)
sleep(1.2)

d1 = Dispatcher(MockBot('disp1'), Queue(), workers=1)
d2 = Dispatcher(MockBot('disp2'), Queue(), workers=1)
d1 = Dispatcher(MockBot('disp1'), Queue())
d2 = Dispatcher(MockBot('disp2'), Queue())
q1 = Queue()
q2 = Queue()
d1._init_async_threads('test_1', workers=1)
d2._init_async_threads('test_2', workers=1)

try:
d1.run_async(get_dispatcher_name, q1)
Expand Down Expand Up @@ -622,9 +624,9 @@ def test_webhook_no_ssl(self):

def test_start_dispatcher_twice(self):
self._setup_updater('', messages=0)
d = self.updater.dispatcher
self.updater.start_polling(0.1)
d.start()
sleep(0.5)
self.updater.dispatcher.start()

def test_bootstrap_retries_success(self):
retries = 3
Expand Down