本文整理汇总了Python中telegram.ext.JobQueue类的典型用法代码示例。如果您正苦于以下问题:Python JobQueue类的具体用法?Python JobQueue怎么用?Python JobQueue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JobQueue类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, bot, dispatcher, database_handler, token):
self.dispatcher = dispatcher
# queue for async jobs
self.job_queue = JobQueue(bot)
# Where to get pictures from: local filesystem(local) or Dropbox storage (DB)
self.pic_source = sr["pic_source"]
self.database_handler = database_handler
super(UserCommandHandler, self).__init__(token, self.database_handler)
self._addHandlers()
if self.pic_source == "DB":
self.DB_file_updater_thread = None # a thread that updates files
self.dropbox_handler = DropboxHandler(self.database_handler)
self._updateDBFiles()
elif self.pic_source == "local":
self.local_cleaner_job = None
self._startLocalCleanerJob()
self._initializeSubscriptionJobs()
开发者ID:Highstaker,项目名称:Picture-sender-telegram-bot,代码行数:25,代码来源:command_handler.py
示例2: __init__
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
job_queue_tick_interval=1.0):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
if bot is not None:
self.bot = bot
else:
self.bot = Bot(token, base_url)
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot, job_queue_tick_interval)
self.__exception_event = Event()
self.dispatcher = Dispatcher(self.bot, self.update_queue, workers, self.__exception_event)
self.last_update_id = 0
self.logger = logging.getLogger(__name__)
self.running = False
self.is_idle = False
self.httpd = None
self.__lock = Lock()
self.__threads = []
""":type: list[Thread]"""
开发者ID:Conectel,项目名称:python-telegram-bot,代码行数:27,代码来源:updater.py
示例3: __init__
def __init__(self, token=None, base_url=None, workers=4, bot=None):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
if bot is not None:
self.bot = bot
else:
# we need a connection pool the size of:
# * for each of the workers
# * 1 for Dispatcher
# * 1 for polling Updater (even if webhook is used, we can spare a connection)
# * 1 for JobQueue
# * 1 for main thread
self._request = Request(con_pool_size=workers + 4)
self.bot = Bot(token, base_url, request=self._request)
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot)
self.__exception_event = Event()
self.dispatcher = Dispatcher(
self.bot,
self.update_queue,
job_queue=self.job_queue,
workers=workers,
exception_event=self.__exception_event)
self.last_update_id = 0
self.logger = logging.getLogger(__name__)
self.running = False
self.is_idle = False
self.httpd = None
self.__lock = Lock()
self.__threads = []
""":type: list[Thread]"""
开发者ID:python-telegram-bot,项目名称:python-telegram-bot,代码行数:34,代码来源:updater.py
示例4: initialize_participants
def initialize_participants(job_queue: JobQueue):
user_map = DataSet()
try:
# Todo: auto-initalize function
db = sqlite3.connect('survey/participants.db')
cursor = db.cursor()
cursor.execute("SELECT * FROM participants ORDER BY (ID)")
participants = cursor.fetchall()
# print(participants)
for row in participants:
user = Participant(row[1], init=False)
user.conditions_ = pickle.loads(row[2])
user.data_set_ = pickle.loads(row[0])
user.timezone_ = row[3]
user.country_ = row[4]
user.gender_ = row[5]
user.language_ = row[6]
user.question_ = row[7]
user.age_ = row[8]
user.day_ = row[9]
user.q_idle_ = row[10]
user.active_ = row[11]
user.block_ = row[12]
user.pointer_ = row[13]
user_map.participants[row[1]] = user
if user.language_ != '':
q_set = user_map.return_question_set_by_language(user.language_)
user.q_set_ = q_set
if user.country_ != '' and user.timezone_ != '' and user.gender_ != '':
user.set_next_block()
next_day = user.set_next_block()
if next_day is None and user.active_ and user.pointer_ > -1:
finished(user, job_queue)
continue
element = user.next_block[2]
day_offset = next_day - user.day_
time_t = calc_block_time(element["time"])
due = calc_delta_t(time_t, day_offset, user.timezone_)
debug('QUEUE', 'next block in ' + str(due) + ' seconds. User: ' + str(user.chat_id_), log=True)
new_job = Job(queue_next, due, repeat=False, context=[user, job_queue])
job_queue.put(new_job)
except sqlite3.Error as error:
print(error)
return user_map
开发者ID:philippfeldner,项目名称:diary-survey-bot,代码行数:47,代码来源:questions.py
示例5: __init__
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
user_sig_handler=None,
request_kwargs=None):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
self.logger = logging.getLogger(__name__)
con_pool_size = workers + 4
if bot is not None:
self.bot = bot
if bot.request.con_pool_size < con_pool_size:
self.logger.warning(
'Connection pool of Request object is smaller than optimal value (%s)',
con_pool_size)
else:
# we need a connection pool the size of:
# * for each of the workers
# * 1 for Dispatcher
# * 1 for polling Updater (even if webhook is used, we can spare a connection)
# * 1 for JobQueue
# * 1 for main thread
if request_kwargs is None:
request_kwargs = {}
if 'con_pool_size' not in request_kwargs:
request_kwargs['con_pool_size'] = con_pool_size
self._request = Request(**request_kwargs)
self.bot = Bot(token, base_url, request=self._request)
self.user_sig_handler = user_sig_handler
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot)
self.__exception_event = Event()
self.dispatcher = Dispatcher(
self.bot,
self.update_queue,
job_queue=self.job_queue,
workers=workers,
exception_event=self.__exception_event)
self.last_update_id = 0
self.running = False
self.is_idle = False
self.httpd = None
self.__lock = Lock()
self.__threads = []
开发者ID:noam09,项目名称:deluge-telegramer,代码行数:52,代码来源:updater.py
示例6: Updater
class Updater(object):
"""
This class, which employs the :class:`telegram.ext.Dispatcher`, provides a frontend to
:class:`telegram.Bot` to the programmer, so they can focus on coding the bot. Its purpose is to
receive the updates from Telegram and to deliver them to said dispatcher. It also runs in a
separate thread, so the user can interact with the bot, for example on the command line. The
dispatcher supports handlers for different kinds of data: Updates from Telegram, basic text
commands and even arbitrary types. The updater can be started as a polling service or, for
production, use a webhook to receive updates. This is achieved using the WebhookServer and
WebhookHandler classes.
Attributes:
bot (:class:`telegram.Bot`): The bot used with this Updater.
user_sig_handler (:obj:`signal`): signals the updater will respond to.
update_queue (:obj:`Queue`): Queue for the updates.
job_queue (:class:`telegram.ext.JobQueue`): Jobqueue for the updater.
dispatcher (:class:`telegram.ext.Dispatcher`): Dispatcher that handles the updates and
dispatches them to the handlers.
running (:obj:`bool`): Indicates if the updater is running.
Args:
token (:obj:`str`, optional): The bot's token given by the @BotFather.
base_url (:obj:`str`, optional): Base_url for the bot.
workers (:obj:`int`, optional): Amount of threads in the thread pool for functions
decorated with ``@run_async``.
bot (:class:`telegram.Bot`, optional): A pre-initialized bot instance. If a pre-initialized
bot is used, it is the user's responsibility to create it using a `Request`
instance with a large enough connection pool.
user_sig_handler (:obj:`function`, optional): Takes ``signum, frame`` as positional
arguments. This will be called when a signal is received, defaults are (SIGINT,
SIGTERM, SIGABRT) setable with :attr:`idle`.
request_kwargs (:obj:`dict`, optional): Keyword args to control the creation of a request
object (ignored if `bot` argument is used).
Note:
You must supply either a :attr:`bot` or a :attr:`token` argument.
Raises:
ValueError: If both :attr:`token` and :attr:`bot` are passed or none of them.
"""
_request = None
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
user_sig_handler=None,
request_kwargs=None):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
self.logger = logging.getLogger(__name__)
con_pool_size = workers + 4
if bot is not None:
self.bot = bot
if bot.request.con_pool_size < con_pool_size:
self.logger.warning(
'Connection pool of Request object is smaller than optimal value (%s)',
con_pool_size)
else:
# we need a connection pool the size of:
# * for each of the workers
# * 1 for Dispatcher
# * 1 for polling Updater (even if webhook is used, we can spare a connection)
# * 1 for JobQueue
# * 1 for main thread
if request_kwargs is None:
request_kwargs = {}
if 'con_pool_size' not in request_kwargs:
request_kwargs['con_pool_size'] = con_pool_size
self._request = Request(**request_kwargs)
self.bot = Bot(token, base_url, request=self._request)
self.user_sig_handler = user_sig_handler
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot)
self.__exception_event = Event()
self.dispatcher = Dispatcher(
self.bot,
self.update_queue,
job_queue=self.job_queue,
workers=workers,
exception_event=self.__exception_event)
self.last_update_id = 0
self.running = False
self.is_idle = False
self.httpd = None
self.__lock = Lock()
self.__threads = []
def _init_thread(self, target, name, *args, **kwargs):
thr = Thread(target=self._thread_wrapper, name=name, args=(target,) + args, kwargs=kwargs)
#.........这里部分代码省略.........
开发者ID:noam09,项目名称:deluge-telegramer,代码行数:101,代码来源:updater.py
示例7: setUp
def setUp(self):
self.jq = JobQueue(MockBot('jobqueue_test'))
self.result = 0
开发者ID:igorcmoura,项目名称:python-telegram-bot,代码行数:3,代码来源:test_jobqueue.py
示例8: setUp
def setUp(self):
self.jq = JobQueue(MockBot('jobqueue_test'))
self.jq.start()
self.result = 0
self.job_time = 0
开发者ID:python-telegram-bot,项目名称:python-telegram-bot,代码行数:5,代码来源:test_jobqueue.py
示例9: job_queue
def job_queue(bot):
jq = JobQueue(bot)
jq.start()
yield jq
jq.stop()
开发者ID:technoekat,项目名称:prodeda1,代码行数:5,代码来源:test_jobqueue.py
示例10: setUp
def setUp(self):
self.jq = JobQueue("Bot", tick_interval=0.005)
self.result = 0
开发者ID:AlexR1712,项目名称:python-telegram-bot,代码行数:3,代码来源:test_jobqueue.py
示例11: JobQueueTest
class JobQueueTest(BaseTest, unittest.TestCase):
"""
This object represents Tests for Updater, Dispatcher, WebhookServer and
WebhookHandler
"""
def setUp(self):
self.jq = JobQueue(MockBot('jobqueue_test'))
self.jq.start()
self.result = 0
self.job_time = 0
def tearDown(self):
if self.jq is not None:
self.jq.stop()
def getSeconds(self):
return int(ceil(time.time()))
def job1(self, bot, job):
self.result += 1
def job2(self, bot, job):
raise Exception("Test Error")
def job3(self, bot, job):
self.result += 1
job.schedule_removal()
def job4(self, bot, job):
self.result += job.context
def job5(self, bot, job):
self.job_time = self.getSeconds()
def test_basic(self):
self.jq.put(Job(self.job1, 0.1))
sleep(1.5)
self.assertGreaterEqual(self.result, 10)
def test_job_with_context(self):
self.jq.put(Job(self.job4, 0.1, context=5))
sleep(1.5)
self.assertGreaterEqual(self.result, 50)
def test_noRepeat(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
sleep(0.5)
self.assertEqual(1, self.result)
def test_nextT(self):
self.jq.put(Job(self.job1, 0.1), next_t=0.5)
sleep(0.45)
self.assertEqual(0, self.result)
sleep(0.1)
self.assertEqual(1, self.result)
def test_multiple(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
self.jq.put(Job(self.job1, 0.2, repeat=False))
self.jq.put(Job(self.job1, 0.4))
sleep(1)
self.assertEqual(4, self.result)
def test_disabled(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.enabled = False
j1.enabled = False
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.schedule_removal()
j1.schedule_removal()
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal_from_within(self):
self.jq.put(Job(self.job1, 0.4))
self.jq.put(Job(self.job3, 0.2))
sleep(1)
self.assertEqual(3, self.result)
def test_longer_first(self):
#.........这里部分代码省略.........
开发者ID:python-telegram-bot,项目名称:python-telegram-bot,代码行数:101,代码来源:test_jobqueue.py
示例12: question_handler
def question_handler(bot: Bot, update: Update, user_map: DataSet, job_queue: JobQueue):
try:
# Get the user from the dict and its question_set (by language)
user = user_map.participants[update.message.chat_id] # type: Participant
# Case for very first question.
if user.question_ == -1:
user.set_active(True)
user.set_language(update.message.text)
user.set_block(0)
q_set = user_map.return_question_set_by_language(user.language_)
user.q_set_ = q_set
current_day = q_set[0]["day"]
user.set_day(current_day)
user.set_block(0)
elif user.q_idle_:
q_set = user.q_set_
# Get the matching question for the users answer.
pointer = user.pointer_
d_prev = q_set[pointer]
b_prev = d_prev["blocks"][user.block_]
q_prev = b_prev["questions"][user.question_]
if not valid_answer(q_prev, update.message.text, user):
user.set_q_idle(True)
return
# Storing the answer and moving on the next question
store_answer(user, update.message.text, q_prev, job_queue)
user.set_q_idle(False)
else:
# User has send something without being asked a question.
return
except KeyError as error:
print(error)
return
if not user.active_:
return
message, question = find_next_question(user)
if question is not None:
message = question["text"]
q_keyboard = get_keyboard(question["choice"], user)
try:
bot.send_message(chat_id=user.chat_id_, text=message, reply_markup=q_keyboard)
debug(flag="MSG", text=str(user.chat_id_) + ": " + message + "\n")
except TelegramError as error:
if error.message == 'Unauthorized':
user.pause()
user.set_q_idle(True)
elif user.auto_queue_ is False:
user.block_complete_ = True
next_day = user.set_next_block()
if next_day is None:
finished(user, job_queue)
return
element = user.next_block[2]
day_offset = next_day - user.day_
time_t = calc_block_time(element["time"])
due = calc_delta_t(time_t, day_offset, user.timezone_)
debug('QUEUE', 'next block in ' + str(due) + ' seconds. User: ' + str(user.chat_id_), log=True)
new_job = Job(queue_next, due, repeat=False, context=[user, job_queue])
user.job_ = new_job
job_queue.put(new_job)
开发者ID:philippfeldner,项目名称:diary-survey-bot,代码行数:68,代码来源:questions.py
示例13: JobQueueTest
class JobQueueTest(BaseTest, unittest.TestCase):
"""
This object represents Tests for Updater, Dispatcher, WebhookServer and
WebhookHandler
"""
def setUp(self):
self.jq = JobQueue("Bot", tick_interval=0.005)
self.result = 0
def tearDown(self):
if self.jq is not None:
self.jq.stop()
def job1(self, bot):
self.result += 1
def job2(self, bot):
raise Exception("Test Error")
def test_basic(self):
self.jq.put(self.job1, 0.1)
sleep(1.5)
self.assertGreaterEqual(self.result, 10)
def test_noRepeat(self):
self.jq.put(self.job1, 0.1, repeat=False)
sleep(0.5)
self.assertEqual(1, self.result)
def test_nextT(self):
self.jq.put(self.job1, 0.1, next_t=0.5)
sleep(0.45)
self.assertEqual(0, self.result)
sleep(0.1)
self.assertEqual(1, self.result)
def test_multiple(self):
self.jq.put(self.job1, 0.1, repeat=False)
self.jq.put(self.job1, 0.2, repeat=False)
self.jq.put(self.job1, 0.4)
sleep(1)
self.assertEqual(4, self.result)
def test_error(self):
self.jq.put(self.job2, 0.1)
self.jq.put(self.job1, 0.2)
self.jq.start()
sleep(0.4)
self.assertEqual(1, self.result)
def test_inUpdater(self):
u = Updater(bot="MockBot", job_queue_tick_interval=0.005)
u.job_queue.put(self.job1, 0.5)
sleep(0.75)
self.assertEqual(1, self.result)
u.stop()
sleep(2)
self.assertEqual(1, self.result)
开发者ID:AlexR1712,项目名称:python-telegram-bot,代码行数:59,代码来源:test_jobqueue.py
示例14: JobQueueTest
class JobQueueTest(BaseTest, unittest.TestCase):
"""
This object represents Tests for Updater, Dispatcher, WebhookServer and
WebhookHandler
"""
def setUp(self):
self.jq = JobQueue(MockBot('jobqueue_test'))
self.jq.start()
self.result = 0
self.job_time = 0
def tearDown(self):
if self.jq is not None:
self.jq.stop()
def job1(self, bot, job):
self.result += 1
def job2(self, bot, job):
raise Exception("Test Error")
def job3(self, bot, job):
self.result += 1
job.schedule_removal()
def job4(self, bot, job):
self.result += job.context
def job5(self, bot, job):
self.job_time = time.time()
def test_basic(self):
self.jq.put(Job(self.job1, 0.1))
sleep(1.5)
self.assertGreaterEqual(self.result, 10)
def test_job_with_context(self):
self.jq.put(Job(self.job4, 0.1, context=5))
sleep(1.5)
self.assertGreaterEqual(self.result, 50)
def test_noRepeat(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
sleep(0.5)
self.assertEqual(1, self.result)
def test_nextT(self):
self.jq.put(Job(self.job1, 0.1), next_t=0.5)
sleep(0.45)
self.assertEqual(0, self.result)
sleep(0.1)
self.assertEqual(1, self.result)
def test_multiple(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
self.jq.put(Job(self.job1, 0.2, repeat=False))
self.jq.put(Job(self.job1, 0.4))
sleep(1)
self.assertEqual(4, self.result)
def test_disabled(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.enabled = False
j1.enabled = False
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.schedule_removal()
j1.schedule_removal()
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal_from_within(self):
self.jq.put(Job(self.job1, 0.4))
self.jq.put(Job(self.job3, 0.2))
sleep(1)
self.assertEqual(3, self.result)
def test_longer_first(self):
self.jq.put(Job(self.job1, 0.2, repeat=False))
self.jq.put(Job(self.job1, 0.1, repeat=False))
sleep(0.15)
#.........这里部分代码省略.........
开发者ID:KenOokamiHoro,项目名称:python-telegram-bot,代码行数:101,代码来源:test_jobqueue.py
示例15: UserCommandHandler
class UserCommandHandler(PicBotRoutines):
"""docstring for UserCommandHandler"""
def _command_method(func):
"""Decorator for functions that are invoked on commands. Ensures that the user is initialized."""
# @functools.wraps(func)
def wrapper(self, bot, update, *args, **kwargs):
# print("command method", func.__name__, ) # debug
# print("self",self)# debug
# print("command method", self, bot, update, args, kwargs, sep="||") # debug
chat_id = update.message.chat_id
log.info("Command method called!", func.__name__, "Chat_id: ", chat_id)
# Initialize user, if not present in DB
self.database_handler.initializeUser(chat_id=chat_id)
log.debug("User initialized")
lS = LanguageSupport(self.database_handler.getLang(chat_id)).languageSupport
# noinspection PyCallingNonCallable
func(self, bot, update, lS)
log.debug("Function completed")
return wrapper
def __init__(self, bot, dispatcher, database_handler, token):
self.dispatcher = dispatcher
# queue for async jobs
self.job_queue = JobQueue(bot)
# Where to get pictures from: local filesystem(local) or Dropbox storage (DB)
self.pic_source = sr["pic_source"]
self.database_handler = database_handler
super(UserCommandHandler, self).__init__(token, self.database_handler)
self._addHandlers()
if self.pic_source == "DB":
self.DB_file_updater_thread = None # a thread that updates files
self.dropbox_handler = DropboxHandler(self.database_handler)
self._updateDBFiles()
elif self.pic_source == "local":
self.local_cleaner_job = None
self._startLocalCleanerJob()
self._initializeSubscriptionJobs()
def _initializeSubscriptionJobs(self):
for chat_id in self.database_handler.getAllSubscribedUserIDs():
log.debug("_initializeSubscriptionJobs chat_id", chat_id)
self.createPeriodicSenderTask(chat_id)
def _updateDBFiles(self, bot=None, job=None):
if not self.DB_file_updater_thread or not self.DB_file_updater_thread.is_alive():
self.DB_file_updater_thread = self.dropbox_handler.updateFiles()
job = Job(self._updateDBFiles, interval=sr['file_update_period'], repeat=False)
else:
log.warning("The Dropbox updater thread hasn't finished yet. Consider increasing FILE_UPDATE_PERIOD in settings!")
job = Job(self._updateDBFiles, interval=10, repeat=False)
# create periodic job
self.job_queue.put(job)
def _startLocalCleanerJob(self):
"""
Creates a delayed async job that cleans database every now and then if local files get deeleted
:return:
"""
log.debug("_startLocalCleanerJob")
self.local_cleaner_job = job = Job(self._localCleanerThread, interval=LOCAL_CLEANER_PERIOD, repeat=True)
self.job_queue.put(job)
def _localCleanerThread(self, bot, job):
log.debug("_localCleanerThread")
local_files = self.getLocalFiles()
bd_files = set(self.database_handler.getFileList())
to_delete = bd_files.difference(local_files)
log.debug("to_delete", to_delete)
if to_delete:
self.database_handler.batchDeleteFiles(to_delete)
def _addHandlers(self):
self.dispatcher.add_handler(CommandHandler('start', self.command_start))
self.dispatcher.add_handler(CommandHandler('help', self.command_help))
self.dispatcher.add_handler(CommandHandler('about', self.command_about))
self.dispatcher.add_handler(CommandHandler('otherbots', self.command_otherbots))
self.dispatcher.add_handler(CommandHandler('gimmepic', self.command_gimmepic))
self.dispatcher.add_handler(CommandHandler('subscribe', self.command_subscribe))
self.dispatcher.add_handler(CommandHandler('unsubscribe', self.command_unsubscribe))
self.dispatcher.add_handler(CommandHandler('spamuncached', self.command_spamuncached))
#.........这里部分代码省略.........
开发者ID:Highstaker,项目名称:Picture-sender-telegram-bot,代码行数:101,代码来源:command_handler.py
示例16: JobQueueTest
class JobQueueTest(BaseTest, unittest.TestCase):
"""
This object represents Tests for Updater, Dispatcher, WebhookServer and
WebhookHandler
"""
def setUp(self):
self.jq = JobQueue("Bot")
self.result = 0
def tearDown(self):
if self.jq is not None:
self.jq.stop()
stop_con_pool()
def job1(self, bot, job):
self.result += 1
def job2(self, bot, job):
raise Exception("Test Error")
def job3(self, bot, job):
self.result += 1
job.schedule_removal()
def job4(self, bot, job):
self.result += job.context
def test_basic(self):
self.jq.put(Job(self.job1, 0.1))
sleep(1.5)
self.assertGreaterEqual(self.result, 10)
def test_job_with_context(self):
self.jq.put(Job(self.job4, 0.1, context=5))
sleep(1.5)
self.assertGreaterEqual(self.result, 50)
def test_noRepeat(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
sleep(0.5)
self.assertEqual(1, self.result)
def test_nextT(self):
self.jq.put(Job(self.job1, 0.1), next_t=0.5)
sleep(0.45)
self.assertEqual(0, self.result)
sleep(0.1)
self.assertEqual(1, self.result)
def test_multiple(self):
self.jq.put(Job(self.job1, 0.1, repeat=False))
self.jq.put(Job(self.job1, 0.2, repeat=False))
self.jq.put(Job(self.job1, 0.4))
sleep(1)
self.assertEqual(4, self.result)
def test_disabled(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.enabled = False
j1.enabled = False
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal(self):
j0 = Job(self.job1, 0.1)
j1 = Job(self.job1, 0.2)
self.jq.put(j0)
self.jq.put(Job(self.job1, 0.4))
self.jq.put(j1)
j0.schedule_removal()
j1.schedule_removal()
sleep(1)
self.assertEqual(2, self.result)
def test_schedule_removal_from_within(self):
self.jq.put(Job(self.job1, 0.4))
self.jq.put(Job(self.job3, 0.2))
sleep(1)
self.assertEqual(3, self.result)
def test_longer_first(self):
self.jq.put(Job(self.job1, 0.2, repeat=False))
self.jq.put(Job(self.job1, 0.1, repeat=False))
sleep(0.15)
self.assertEqual(1, self.result)
def test_error(self):
self.jq.put(Job(self.job2, 0.1))
#.........这里部分代码省略.........
开发者ID:9kopb,项目名称:python-telegram-bot,代码行数:101,代码来源:test_jobqueue.py
示例17: Updater
class Updater(object):
"""
This class, which employs the Dispatcher class, provides a frontend to
telegram.Bot to the programmer, so they can focus on coding the bot. Its
purpose is to receive the updates from Telegram and to deliver them to said
dispatcher. It also runs in a separate thread, so the user can interact
with the bot, for example on the command line. The dispatcher supports
handlers for different kinds of data: Updates from Telegram, basic text
commands and even arbitrary types.
The updater can be started as a polling service or, for production, use a
webhook to receive updates. This is achieved using the WebhookServer and
WebhookHandler classes.
Attributes:
Args:
token (Optional[str]): The bot's token given by the @BotFather
base_url (Optional[str]):
workers (Optional[int]): Amount of threads in the thread pool for
functions decorated with @run_async
bot (Optional[Bot]):
job_queue_tick_interval(Optional[float]): The interval the queue should
be checked for new tasks. Defaults to 1.0
Raises:
ValueError: If both `token` and `bot` are passed or none of them.
"""
def __init__(self,
token=None,
base_url=None,
workers=4,
bot=None,
job_queue_tick_interval=1.0):
if (token is None) and (bot is None):
raise ValueError('`token` or `bot` must be passed')
if (token is not None) and (bot is not None):
raise ValueError('`token` and `bot` are mutually exclusive')
if bot is not None:
self.bot = bot
else:
self.bot = Bot(token, base_url)
self.update_queue = Queue()
self.job_queue = JobQueue(self.bot, job_queue_tick_interval)
self.__exception_event = Event()
self.dispatcher = Dispatcher(self.bot, self.update_queue, workers, self.__exception_event)
self.last_update_id = 0
self.logger = logging.getLogger(__name__)
self.running = False
self.is_idle = False
self.httpd = None
self.__lock = Lock()
self.__threads = []
""":type: list[Thread]"""
def _init_thread(self, target, name, *args, **kwargs):
thr = Thread(target=self._thread_wrapper, name=name, args=(target,) + args, kwargs=kwargs)
thr.start()
self.__threads.append(thr)
def _thread_wrapper(self, target, *args, **kwargs):
thr_name = current_thread().name
self.logger.debug('{0} - started'.format(thr_name))
try:
target(*args, **kwargs)
except Exception:
self.__exception_event.set()
self.logger.exception('unhandled exception')
raise
self.logger.debug('{0} - ended'.format(thr_name))
def start_polling(self,
poll_interval=0.0,
timeout=10,
network_delay=2,
clean=False,
bootstrap_retries=0):
"""
Starts polling updates from Telegram.
Args:
poll_interval (Optional[float]): Time to wait between polling
updates from Telegram in seconds. Default is 0.0
timeout (Optional[float]): Passed to Bot.getUpdates
network_delay (Optional[float]): Passed to Bot.getUpdates
clean (Optional[bool]): Whether to clean any pending updates on
Telegram servers before actually starting to poll. Default is
False.
bootstrap_retries (Optional[int[): Whether the bootstrapping phase
of the `Updater` will retry on failures on the Telegram server.
| < 0 - retry indefinitely
| 0 - no retries (default)
| > 0 - retry up to X times
Returns:
Queue: The update queue that can be filled from the main thread
#.........这里部分代码省略.........
开发者ID:Conectel,项目名称:python-telegram-bot,代码行数:101,代码来源:updater.py
示例18: setUp
def setUp(self):
self.jq = JobQueue("Bot")
self.result = 0
开发者ID:9kopb,项目名称:python-telegram-bot,代码行数:3,代码来源:test_jobqueue.py
注:本文中的telegram.ext.JobQueue类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论