aiogram / aiogram

aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
https://aiogram.dev
MIT License
4.75k stars 835 forks source link

Polling task created but will stopped never #271

Closed goDeni closed 4 years ago

goDeni commented 4 years ago

Pooling task created but will stopped never This is not True

creating task - https://github.com/aiogram/aiogram/blob/6831962e3cf8f5a36d8ef6ea046b5044b1e59b6a/aiogram/utils/executor.py#L306

error: Exception ignored in: <coroutine object Executor._startup_polling at 0x7f67e40755f0> Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/aiogram/utils/executor.py", line 356, in _startup_polling await self._welcome() File "/usr/local/lib/python3.7/site-packages/aiogram/utils/executor.py", line 346, in _welcome user = await self.dispatcher.bot.me File "/usr/local/lib/python3.7/site-packages/aiogram/bot/bot.py", line 27, in me setattr(self, '_me', await self.get_me()) File "/usr/local/lib/python3.7/site-packages/aiogram/bot/bot.py", line 176, in get_me result = await self.request(api.Methods.GET_ME, payload) File "/usr/local/lib/python3.7/site-packages/aiogram/bot/base.py", line 194, in request proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, kwargs) File "/usr/local/lib/python3.7/site-packages/aiogram/bot/api.py", line 103, in make_request async with session.post(url, data=req, kwargs) as response: File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1012, in aenter self._resp = await self._coro File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 483, in _request timeout=real_timeout File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 523, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 859, in _create_connection req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 986, in _create_direct_connection req=req, client_error=client_error) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa File "/usr/local/lib/python3.7/asyncio/base_events.py", line 985, in create_connection ssl_handshake_timeout=ssl_handshake_timeout) File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1015, in _create_connection_transport transport.close() File "/usr/local/lib/python3.7/asyncio/sslproto.py", line 317, in close self._ssl_protocol._start_shutdown() File "/usr/local/lib/python3.7/asyncio/sslproto.py", line 588, in _start_shutdown self._abort() File "/usr/local/lib/python3.7/asyncio/sslproto.py", line 725, in _abort self._transport.abort() File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 643, in abort self._force_close(None) File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 695, in _force_close self._loop.call_soon(self._call_connection_lost, exc) File "/usr/local/lib/python3.7/asyncio/base_events.py", line 687, in call_soon self._check_closed() File "/usr/local/lib/python3.7/asyncio/base_events.py", line 479, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed [ERROR ] 2020-02-03 08:56:26,629 [7 ]MainThread@'asyncio.default_exception_handler:1615': Task was destroyed but it is pending!

evgfilim1 commented 4 years ago

I haven't faced this error ever. Please, share your configuration (can be obtained by python -m aiogram) and a snippet where this error happens.

Olegt0rr commented 4 years ago

@goDeni,

  1. Don't confuse polling and pooling. In that case - polling.
  2. Why don't you save the task and cancel it when the application shuts down?
goDeni commented 4 years ago

@goDeni,

  1. Don't confuse polling and pooling. In that case - polling.
  2. Why don't you save the task and cancel it when the application shuts down?

Because function doesn't return task object In my opinion function have to do this

Olegt0rr commented 4 years ago

You should read it: https://docs.python.org/3.7/library/asyncio-eventloop.html#asyncio.loop.create_task

goDeni commented 4 years ago

You should read it: https://docs.python.org/3.7/library/asyncio-eventloop.html#asyncio.loop.create_task

=) I meant function "start_polling"

Olegt0rr commented 4 years ago
  1. You can stop polling by Dispatcher methods stop_polling and wait_closed.
    
    import asyncio as aio
    from aiogram import Bot, Dispatcher

TOKEN = 'your token'

loop = aio.get_event_loop() bot = Bot(token=TOKEN) dp = Dispatcher(bot=bot)

async def main():

start polling in task

loop.create_task(dp.start_polling())

# do something
await do_something()

# stop app
await on_shutdown()

async def do_something(): await aio.sleep(5) print('Done.')

async def on_shutdown(): print("Can we stop it?") dp.stop_polling() await dp.wait_closed() await bot.close() print("Yes, we can!")

if name == 'main': loop.run_until_complete(main()) loop.close()


2. You can stop polling by cancelling task.
```python
task = loop.create_task(dp.start_polling())
task.cancel()

But don't forget to handle other task pending in your loop. To check it use basic features of event loop described in python docs.