implement starting jobqueue in thread and fix errors

This commit is contained in:
Jannes Höke 2016-01-04 00:01:00 +01:00
parent 6f76bfc2de
commit 68c844df0c
2 changed files with 20 additions and 9 deletions

View file

@ -49,10 +49,11 @@ from .update import Update
from .bot import Bot
from .dispatcher import Dispatcher
from .updater import Updater
from .jobqueue import JobQueue
__all__ = ['Bot', 'Updater', 'Dispatcher', 'Emoji', 'TelegramError',
'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide',
'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction',
'Location', 'Contact', 'Video', 'Sticker', 'Document', 'File',
'Audio', 'PhotoSize', 'Chat', 'Update', 'ParseMode', 'Message',
'User', 'TelegramObject', 'NullHandler', 'Voice']
'User', 'TelegramObject', 'NullHandler', 'Voice', 'JobQueue']

View file

@ -22,7 +22,7 @@ This module contains the class JobQueue
import logging
import time
from threading import Lock
from threading import Thread, Lock
try:
from queue import Queue, PriorityQueue
@ -76,6 +76,8 @@ class JobQueue(object):
if next_t is None:
next_t = interval
next_t += time.time()
self.logger.debug("Putting a {} with t={}".format(
job.name, next_t))
self.queue.put((next_t, job))
@ -95,8 +97,8 @@ class JobQueue(object):
if t < now:
self.queue.get()
self.logger.debug("About time! running")
j.run()
self.put(j, now + j.interval)
j.run(self.bot)
self.put(j.run, j.interval)
continue
self.logger.debug("Next task isn't due yet. Finished!")
@ -104,18 +106,26 @@ class JobQueue(object):
def start(self):
"""
Thread target of thread 'job_queue'. Runs in background and performs
ticks on the job queue.
Starts the job_queue thread.
"""
self.__lock.acquire()
if not self.running:
self.running = True
self.__lock.release()
job_queue_thread = Thread(target=self._start,
name="job_queue")
job_queue_thread.start()
else:
self.__lock.release()
def _start(self):
"""
Thread target of thread 'job_queue'. Runs in background and performs
ticks on the job queue.
"""
while self.running:
self.tick()
time.sleep(self.tick_interval)
else:
self.__lock.release()
def stop(self):
"""