如何在 python-telegram-bot 中运行并发

问题描述 投票:0回答:1

这是我的代码

import logging
import requests
from telegram import Update
from telegram.ext import ApplicationBuilder, CallbackContext, CommandHandler

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

async def start(update: Update, context: CallbackContext):
    await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

async def start(update: Update, context: CallbackContext):
    # Some code

async def complete(update: Update, context: CallbackContext):
    # Some code

if __name__ == '__main__':
    application = ApplicationBuilder().token('').build()

    start_handler = CommandHandler('start', start, block=False)
    application.add_handler(start_handler)

    register_handler = CommandHandler('register', register, block=False)
    application.add_handler(register_handler)

    complete_handler = CommandHandler('complete', complete, block=False)
    application.add_handler(complete_handler)

    application.run_polling()

比如我运行/register hello,然后运行/complete bye,complete命令只在register命令完成时运行。我需要完整的命令在我输入时立即运行,不要等待注册命令。

我检查了文档和 wiki (https://github.com/python-telegram-bot/python-telegram-bot/wiki/Concurrency) 但还是不行。

请帮助我。谢谢

比如我运行/register hello,然后运行/complete bye,complete命令只在register命令完成时运行。我需要完整的命令在我输入时立即运行,不要等待注册命令。

python concurrency python-telegram-bot
1个回答
0
投票

PTB支持组也有人问过这个问题:https://t.me/pythontelegrambotgroup/661349 answer 是 OP 在回调中使用了 blocking code(如非异步

requests
库的用法所示)。 PTB v20.x+ 构建于
asyncio
上,因此不处理并发方面的阻塞代码。

PS:请不要同时在多个地方问同一个问题。当因为你在另一个地方没有得到问题而在某个地方重复一个问题时,至少包括指向你提出问题的所有地方的链接。


免责声明:我目前是

python-telegram-bot

的维护者
© www.soinside.com 2019 - 2024. All rights reserved.