【发布时间】:2021-10-20 17:34:10
【问题描述】:
我正在尝试在 google colab plus 上运行 Telethon 代码并让它在后台运行。代码运行良好,在浏览器选项卡中启动和运行时没有错误。当我关闭选项卡并重新打开它时,它在输出控制台中显示太多重复错误,如下所示
RuntimeError: cannot enter context: <Context object at 0x7f0063bc04b0> is already entered
Exception in callback BaseAsyncIOLoop._handle_events(18, 1)
handle: <Handle BaseAsyncIOLoop._handle_events(18, 1)>
Traceback (most recent call last):
我在 colab pro plus notebook 上运行的代码
from telethon.sync import TelegramClient, events, Button
from telethon.sessions import StringSession
from telethon import functions, types
import asyncio
import nest_asyncio
nest_asyncio.apply()
api_id =
api_hash = ''
session=''
async def main():
async with TelegramClient(StringSession(session), api_id, api_hash) as client:
print("Inside")
@client.on(events.NewMessage(chats=[xxxxxxxxxx]))
async def my_event_handler(event):
print(event.raw_text)
while True:
#doSomething and wait for 0.03 seconds
await asyncio.sleep(0.03)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
我无法理解导致错误的原因,因为只有在我关闭并重新打开运行代码的笔记本时才会显示错误
【问题讨论】:
标签: jupyter-notebook jupyter python-asyncio