【发布时间】:2021-05-15 01:35:45
【问题描述】:
我正在尝试同时收听电报或不和谐消息,无论是第一个进来的消息。对于 Telegram,我正在使用 Telethon:
async def listentg():
tgclient = TelegramClient('anon', conntg.tg_api_id, conntg.tg_api_hash)
@tgclient.on(events.NewMessage(chats=conntg.canaltg, pattern=patternmatch))
async def tg_event_handler(event):
print("Telegram message listened")
await tgclient.disconnect()
await tgclient.start()
await tgclient.run_until_disconnected()
对于 Discord,我使用的是 Discum
async def listendc():
dcclient = discum.Client(token=conndc.tokendc, log=False)
@dcclient.gateway.command
def dc_event_handler(resp):
if resp.event.message:
print("Discord message listened")
dcclient.gateway.close()
dcclient.gateway.run()
我知道要同时运行 CPU 阻塞代码,我必须 (https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor) use python processes,但我不知道该怎么做,只是等待第一个返回值。
【问题讨论】:
-
如果
discum阻塞了整个线程,那么是的,你需要loop.run_in_executor。使您的def listendc非异步并使用run_in_executor就足够了;你遇到了什么问题?
标签: python python-3.x discord telethon