Typo 'pooling' -> 'polling'.

This commit is contained in:
Alex Root Junior 2017-11-21 00:53:53 +02:00
parent 233acab68e
commit ef3b59c63c
10 changed files with 74 additions and 65 deletions

View file

@ -11,12 +11,13 @@ from .webhook import BaseResponse
from ..bot import Bot
from ..types.message import ContentType
from ..utils import context
from ..utils.deprecated import deprecated
from ..utils.exceptions import NetworkError, TelegramAPIError
log = logging.getLogger(__name__)
MODE = 'MODE'
LONG_POOLING = 'long-pooling'
LONG_POLLING = 'long-polling'
UPDATE_OBJECT = 'update_object'
@ -30,7 +31,7 @@ class Dispatcher:
"""
def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage] = None,
run_tasks_by_default: bool=False):
run_tasks_by_default: bool = False):
if loop is None:
loop = bot.loop
if storage is None:
@ -58,10 +59,10 @@ class Dispatcher:
self.errors_handlers = Handler(self, once=False)
self._pooling = False
self._polling = False
def __del__(self):
self._pooling = False
self._polling = False
@property
def data(self):
@ -196,9 +197,20 @@ class Dispatcher:
return await self.bot.delete_webhook()
async def start_pooling(self, timeout=20, relax=0.1, limit=None, reset_webhook=True):
@deprecated('The old method was renamed to `start_polling`')
async def start_pooling(self, *args, **kwargs):
"""
Start long-pooling
Start long-lopping
:param args:
:param kwargs:
:return:
"""
return await self.start_polling(*args, **kwargs)
async def start_polling(self, timeout=20, relax=0.1, limit=None, reset_webhook=True):
"""
Start long-polling
:param timeout:
:param relax:
@ -206,21 +218,21 @@ class Dispatcher:
:param reset_webhook:
:return:
"""
if self._pooling:
raise RuntimeError('Pooling already started')
if self._polling:
raise RuntimeError('Polling already started')
log.info('Start pooling.')
log.info('Start polling.')
context.set_value(MODE, LONG_POOLING)
context.set_value(MODE, LONG_POLLING)
context.set_value('dispatcher', self)
context.set_value('bot', self.bot)
if reset_webhook:
await self.reset_webhook(check=True)
self._pooling = True
self._polling = True
offset = None
while self._pooling:
while self._polling:
try:
updates = await self.bot.get_updates(limit=limit, offset=offset, timeout=timeout)
except NetworkError:
@ -232,16 +244,16 @@ class Dispatcher:
log.info("Received {0} updates.".format(len(updates)))
offset = updates[-1].update_id + 1
self.loop.create_task(self._process_pooling_updates(updates))
self.loop.create_task(self._process_polling_updates(updates))
if relax:
await asyncio.sleep(relax)
log.warning('Pooling is stopped.')
log.warning('Polling is stopped.')
async def _process_pooling_updates(self, updates):
async def _process_polling_updates(self, updates):
"""
Process updates received from long-pooling.
Process updates received from long-polling.
:param updates: list of updates.
"""
@ -257,22 +269,30 @@ class Dispatcher:
except TelegramAPIError:
log.exception('Cause exception while processing updates.')
@deprecated('The old method was renamed to `stop_polling`')
def stop_pooling(self):
return self.stop_polling()
def stop_polling(self):
"""
Break long-pooling process.
Break long-polling process.
:return:
"""
if self._pooling:
log.info('Stop pooling.')
self._pooling = False
if self._polling:
log.info('Stop polling.')
self._polling = False
@deprecated('The old method was renamed to `is_polling`')
def is_pooling(self):
return self.is_polling()
def is_polling(self):
"""
Check pooling is enabled?
Check polling is enabled?
:return:
"""
return self._pooling
return self._polling
def register_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None,
state=None, custom_filters=None, run_task=None, **kwargs):

View file

@ -1,11 +1,10 @@
import asyncio
from aiohttp import web
from aiogram.bot.api import log
from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import BOT_DISPATCHER_KEY, get_new_configured_app
from aiogram.utils import context
from . import context
from .deprecated import deprecated
from ..bot.api import log
from ..dispatcher import Dispatcher
from ..dispatcher.webhook import BOT_DISPATCHER_KEY, get_new_configured_app
async def _startup(dispatcher: Dispatcher, skip_updates=False, callback=None):
@ -33,8 +32,8 @@ async def _shutdown(dispatcher: Dispatcher, callback=None):
if callable(callback):
await callback(dispatcher)
if dispatcher.is_pooling():
dispatcher.stop_pooling()
if dispatcher.is_polling():
dispatcher.stop_polling()
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
@ -46,8 +45,13 @@ async def _wh_shutdown(app):
await _shutdown(dispatcher, callback=callback)
def start_pooling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, on_shutdown=None):
log.warning('Start bot with long-pooling.')
@deprecated('The old function was renamed to `start_polling`')
def start_pooling(*args, **kwargs):
return start_polling(*args, **kwargs)
def start_polling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, on_shutdown=None):
log.warning('Start bot with long-polling.')
if loop is None:
loop = dispatcher.loop
@ -55,7 +59,7 @@ def start_pooling(dispatcher, *, loop=None, skip_updates=False, on_startup=None,
try:
loop.run_until_complete(_startup(dispatcher, skip_updates=skip_updates, callback=on_startup))
loop.create_task(dispatcher.start_pooling(reset_webhook=True))
loop.create_task(dispatcher.start_polling(reset_webhook=True))
loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass

View file

@ -29,12 +29,12 @@ And next: all bots is needed command for starting interaction with bot. Registe
async def send_welcome(message: types.Message):
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
And last step - run long pooling.
And last step - run long polling.
.. code-block:: python3
if __name__ == '__main__':
executor.start_pooling(dp, on_startup=startup)
executor.start_polling(dp, on_startup=startup)
Summary
-------
@ -49,5 +49,4 @@ Summary
dp = Dispatcher(bot)
if __name__ == '__main__':
executor.start_pooling(dp)
executor.start_polling(dp)

View file

@ -10,7 +10,7 @@ Provided to start bot with webhook:
--host-name example.com \
--webhook-port 443
Or long pooling:
Or long polling:
python adwanced_executor_example.py --token TOKEN_HERE
So... In this example found small trouble:
@ -29,7 +29,7 @@ import sys
from aiogram import Bot
from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import *
from aiogram.utils.executor import start_pooling, start_webhook
from aiogram.utils.executor import start_polling, start_webhook
logging.basicConfig(level=logging.INFO)
@ -120,7 +120,7 @@ def main(arguments):
on_shutdown=on_shutdown,
host=host, port=port, path=sock, ssl_context=ssl_context)
else:
start_pooling(dispatcher, on_startup=on_startup, on_shutdown=on_shutdown)
start_polling(dispatcher, on_startup=on_startup, on_shutdown=on_shutdown)
if __name__ == '__main__':

View file

@ -8,7 +8,7 @@ import logging
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode
from aiogram.utils.executor import start_pooling
from aiogram.utils.executor import start_polling
from aiogram.utils.markdown import *
API_TOKEN = 'BOT TOKEN HERE'
@ -33,11 +33,5 @@ async def check_language(message: types.Message):
sep='\n'), parse_mode=ParseMode.MARKDOWN)
async def main():
count = await dp.skip_updates()
print(f"Skipped {count} updates.")
await dp.start_pooling()
if __name__ == '__main__':
start_pooling(dp, loop=loop, skip_updates=True)
start_polling(dp, loop=loop, skip_updates=True)

View file

@ -3,7 +3,7 @@ import logging
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils.executor import start_pooling
from aiogram.utils.executor import start_polling
API_TOKEN = 'BOT TOKEN HERE'
@ -32,7 +32,7 @@ async def echo(message: types.Message):
if __name__ == '__main__':
start_pooling(dp, loop=loop, skip_updates=True)
start_polling(dp, loop=loop, skip_updates=True)
# Also you can use another execution method
# >>> try:

View file

@ -128,4 +128,4 @@ async def shutdown(dispatcher: Dispatcher):
if __name__ == '__main__':
executor.start_pooling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown)
executor.start_polling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown)

View file

@ -3,7 +3,7 @@ import logging
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils.executor import start_pooling
from aiogram.utils.executor import start_polling
API_TOKEN = 'BOT TOKEN HERE'
@ -22,4 +22,4 @@ async def inline_echo(inline_query: types.InlineQuery):
if __name__ == '__main__':
start_pooling(dp, loop=loop, skip_updates=True)
start_polling(dp, loop=loop, skip_updates=True)

View file

@ -95,4 +95,4 @@ async def got_payment(message: types.Message):
if __name__ == '__main__':
executor.start_pooling(dp, loop=loop)
executor.start_polling(dp, loop=loop)

View file

@ -1,14 +1,12 @@
import asyncio
import logging
import aiohttp
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode
from aiogram.utils.emoji import emojize
from aiogram.utils.executor import start_pooling
from aiogram.utils.markdown import text, bold, italic, code
from aiogram.utils.executor import start_polling
from aiogram.utils.markdown import bold, code, italic, text
# Configure bot here
API_TOKEN = 'BOT TOKEN HERE'
@ -60,11 +58,5 @@ async def cmd_start(message: types.Message):
# For example emojize('Moon face :new_moon_face:') is represents to 'Moon face 🌚'
async def main():
count = await dp.skip_updates()
print(f"Skipped {count} updates.")
await dp.start_pooling()
if __name__ == '__main__':
start_pooling(dp, loop=loop, skip_updates=True)
start_polling(dp, loop=loop, skip_updates=True)