Request对象连接池小于最优值(8)问题求助
Telegram机器人连接池优化问题
我在开发Telegram机器人时遇到如下问题:Request对象的连接池小于最优值(8)。我尝试创建带有更大连接池的Session对象但无效,问题仍存在,已检查所需依赖库,附上相关代码(已移除机器人令牌),恳请提供其他解决思路。
import os import requests import telegram from telegram.ext import Updater, CommandHandler, MessageHandler, Filters # Replace YOUR_TOKEN_HERE with your actual bot token bot = telegram.Bot(token='') # Set up the Updater and Dispatcher updater = Updater(bot=bot, use_context=True) dispatcher = updater.dispatcher # Set up a global variable to store the watermark text watermark_text = None # Set up a global variable to store the watermark text opacity (default to 50) watermark_opacity = 50 def start(update, context): """Handler for the /start command""" context.bot.send_message(chat_id=update.effective_chat.id, text="Hi! I'm a bot that can add watermark text to videos. Use the /setwatermark command to set the watermark text, and the /opacity command to set the text opacity. Use the /watermark command to watermark a video.") def set_watermark(update, context): """Handler for the /setwatermark command""" # Declare the watermark_text variable as global global watermark_text # Get the watermark text from the user's message watermark_text = " ".join(context.args) # Save the watermark text to the global variable watermark_text = watermark_text context.bot.send_message(chat_id=update.effective_chat.id, text="Watermark text set! You can now use the /watermark command to watermark a video.") def set_opacity(update, context): """Handler for the /opacity command""" # Declare the watermark_opacity variable as global global watermark_opacity # Get the watermark text opacity from the user's message watermark_opacity = int(context.args[0]) def watermark(update, context): """Handler for the /watermark command""" # Check if a watermark text has been set if watermark_text is None: context.bot.send_message(chat_id=update.effective_chat.id, text="No watermark text has been set. Use the /setwatermark command to set the watermark text.") return # Create a Session object with a larger connection pool size session = requests.Session() adapter = requests.adapters.HTTPAdapter(max_pool_size=8) session.mount('https://', adapter) # Get the file ID of the video sent by the user file_id = update.message.video.file_id # Download the video file using the Session object file = session.get(bot.get_file(file_id).file_path) open('input.mp4', 'wb').write(file.content) # Watermark the video using ffmpeg os.system(f'ffmpeg -i input.mp4 -vf "drawtext=fontfile=arial.ttf: text={watermark_text}: fontcolor=white: fontsize=48: x=10: y=10: alpha={watermark_opacity}" output.mp4') # Send the watermarked video back to the user context.bot.send_video(chat_id=update.effective_chat.id, video=open('output.mp4', 'rb')) # Set up the command handlers start_handler = CommandHandler('start', start) dispatcher.add_handler(start_handler) set_watermark_handler = CommandHandler('setwatermark', set_watermark) dispatcher.add_handler(set_watermark_handler) set_opacity_handler = CommandHandler('opacity', set_opacity) dispatcher.add_handler(set_opacity_handler) watermark_handler = CommandHandler('watermark', watermark) dispatcher.add_handler(watermark_handler) # Start the bot updater.start_polling()
解决思路
修复Session的缩进与作用域问题
你代码里创建Session的部分缩进错误,位于return语句之后,实际不会被执行。而且每次请求新建Session无法复用连接池,应该把Session改为全局单例,在代码开头初始化:# 全局初始化Session,复用连接池 session = requests.Session() adapter = requests.adapters.HTTPAdapter(max_pool_size=8) session.mount('https://', adapter)之后在
watermark函数里直接使用这个全局Session即可,无需重复创建。给Bot对象绑定自定义Session
当前仅在下载文件时用了自定义Session,但bot.get_file()本身的请求用的是telegram库默认Session,这部分也会占用连接资源。初始化Bot时传入自定义Session,让所有Bot API请求复用同一个连接池:# 先初始化Session,再传入Bot session = requests.Session() adapter = requests.adapters.HTTPAdapter(max_pool_size=8) session.mount('https://', adapter) bot = telegram.Bot(token='YOUR_TOKEN_HERE', session=session)调整Updater轮询的连接池大小
start_polling()默认有自己的连接配置,通过pool_size参数设置为匹配的最优值:updater.start_polling(pool_size=8)验证连接池配置
可以打印Session适配器的参数确认配置生效:print(session.adapters['https://'].max_pool_size)输出为8则说明连接池设置正确。
内容的提问来源于stack exchange,提问作者Pastel
相关产品推荐
相关产品推荐

