RuntimeError:事件循环已关闭-电报

时间:2018-07-27 09:26:09

标签: python-3.x telegram python-asyncio telethon

我正在尝试使用异步模块(如第15页的documentation中的)运行电报客户端

import telethon
from telethon import TelegramClient, events
import asyncio

client = TelegramClient(USERNAME, API_ID, API_HASH)

async def main():
    await client.start()

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

尝试运行await client.start()时,出现以下错误。

Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._send_loop() running at 
/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py:347> wait_for=<Future pending cb=[Task._wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._recv_loop() running at /home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py:411> wait_for=<Future pending cb=[Task._wakeup()]>>
Exception ignored in: <coroutine object MTProtoSender._send_loop at 0x7f27c688b468>
Traceback (most recent call last):
  File "/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py", line 347, in _send_loop
  File "/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py", line 720, in get
  File "/usr/lib/python3.5/asyncio/queues.py", line 170, in get
  File "/usr/lib/python3.5/asyncio/futures.py", line 227, in cancel
  File "/usr/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<UpdateMethods._update_loop() running at /home/myuser/.local/lib/python3.5/site-packages/telethon/client/updates.py:206> wait_for=<Future pending cb=[Task._wakeup()]>>

是否有任何方法可以解决此问题而无需进入模块代码,或者我对asyncio做错了什么?

1 个答案:

答案 0 :(得分:0)

api_id = your id
api_hash = ''
bot_token = ''

with TelegramClient('session name', api_id, api_hash).start(bot_token=bot_token) as client:
     client.send_message('id', message)

如果您不使用漫游器并使用您的帐户,请删除启动漫游器代码,并且也不需要漫游器令牌

使用Telethon的新闻版本并进行测试(telethon,telethon.sync)

,如果它不能帮助您使用芹菜而它对您有用 我在django项目中使用它并开始工作