【发布时间】:2020-06-10 09:52:08
【问题描述】:
我在尝试运行Telethon documentation 提供的基本代码时收到以下错误。我很困惑为什么我还没有建立一个循环。
RuntimeError: 如果事件循环正在运行,您必须使用“async with”(即您在“async def”中)
我在 spyder 4.0.1 中使用 python 3.7.7
from telethon.sync import TelegramClient
from telethon import functions, types
def channel_info(username, api_id, api_hash):
with TelegramClient(username, api_id, api_hash,channel) as client:
result = client(functions.channels.GetFullChannelRequest(
channel=channel
))
return(result)
out = channel_info(username, api_id, api_hash)
【问题讨论】:
-
> 因为我还没有建立循环。默认情况下,
asyncio在主线程上创建一个事件循环,Anaconda/Spyder/IPython 等工具会自动为您运行该循环,这使您可以直接在解释器中使用async with、async for和await. -
这是非常重要的一点。如果您是包的开发者,请在包文档中明确提及。