I can’t understand what’s wrong with my loop/asyncio when trying to run a telethon script
I’ve tried multiple approaches, but I constatly get errors when trying to run the client with asyncio – so with all functions running at the same time forever 24/7
async def main():
await bot.send_message('me', 'Using asyncio!')
# and other functions of my own. i.e
# fetch_messages() etc
asyncio.run(main())
raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)')
I don’t want to make the post longer than it has to be so I’ll skip all the other stuff I’ve tried because it failed :/
What I want to achieve is to have async def main() – and under them have all the other functions of mine I want to be runned asynchronously, and run them in a loop forever 24/7.
So far from what I’ve learned it should be done with asyncio.run(main()), and not client.run_forever or client.loop.something, but eh, none of them work for me anyway, so I have no idea what approach is the correct one
Full traceback:
Traceback (most recent call last):
File "C:Usersname123PycharmProjectsTG_BOTTG_BOTmain.py", line 55, in <module>
asyncio.run(main())
File "C:Usersname123AppDataLocalProgramsPythonPython38libasynciorunners.py", line 44, in run
return loop.run_until_complete(main)
File "C:Usersname123AppDataLocalProgramsPythonPython38libasynciobase_events.py", line 616, in run_until_complete
return future.result()
File "C:Usersname123PycharmProjectsTG_BOTTG_BOTmain.py", line 52, in main
await bot.start()
File "C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonclientauth.py", line 141, in _start
me = await self.get_me()
File "C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonclientusers.py", line 165, in get_me
me = (await self(
File "C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonclientusers.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonclientusers.py", line 34, in _call
raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)')
RuntimeError: The asyncio event loop must not change after connection (see the FAQ for details)
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000029B76F2DC0>
Traceback (most recent call last):
File "C:Usersname123AppDataLocalProgramsPythonPython38libasyncioproactor_events.py", line 116, in __del__
File "C:Usersname123AppDataLocalProgramsPythonPython38libasyncioproactor_events.py", line 108, in close
File "C:Usersname123AppDataLocalProgramsPythonPython38libasynciobase_events.py", line 719, in call_soon
File "C:Usersname123AppDataLocalProgramsPythonPython38libasynciobase_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection._send_loop() running at C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonnetworkconnectionconnection.py:322> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000029B92CD070>()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<Connection._recv_loop() running at C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonnetworkconnectionconnection.py:341> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000029B8AB1FD0>()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<MTProtoSender._send_loop() running at C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonnetworkmtprotosender.py:464> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000029B92CD790>()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-6' coro=<MTProtoSender._recv_loop() running at C:Usersname123PycharmProjectsTG_BOTTG_BOT.venvlibsite-packagestelethonnetworkmtprotosender.py:507> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x0000029B8AB1E80>()]>>
Unexpected exception in the send loop
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object Connection._send_loop at 0x0000029B84B2040>
RuntimeError: coroutine ignored GeneratorExit
Unhandled error while receiving data
Traceback (most recent call last):
File "C:Usersname123AppDataLocalProgramsPythonPython38libasyncioqueues.py", line 163, in get
await getter
GeneratorExit
user28854634 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1