在FireStore中读/写时,Telegram bot中出现超时错误

时间:2019-07-17 12:34:46

标签: python google-cloud-platform google-cloud-firestore google-cloud-functions python-telegram-bot

我正在用Python编写电报机器人,并将部署到GCP Cloud Function。

该机器人主要在Firestore中处理人员请求和读取/写入数据。

由于最大值Firestore的连接限制为20,如果同时有20个以上的请求,则会发生超时错误。

我增加了工人数量,但没有工作。

请帮助。

import os
import configparser
import logging

import telegram
from flask import Flask, request
from telegram.ext import Updater, Dispatcher, MessageHandler, Filters, CommandHandler
import datetime
import time

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


# Load data from config.ini file
config = configparser.ConfigParser()
config.read('config-dev.ini')


# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
  'projectId': config['TELEGRAM']['GCP_ID'],
})


db = firestore.client()



# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)


# Initial Flask app
app = Flask(__name__)

# Initial bot by Telegram access token
bot = telegram.Bot(token=(config['TELEGRAM']['ACCESS_TOKEN']))

main_chatid = config['TELEGRAM']['MAIN_CHATID']


request_timeout = 10000


@app.route('/hook', methods=['POST'])
def webhook_handler(req):
    """Set route /hook with POST method will trigger this method."""
    if request.method == "POST":
        update = telegram.Update.de_json(request.get_json(force=True), bot)
        payload = str(update)

        print(payload)
        logger.debug(payload)

        # Update dispatcher process that handler to process this message
        dispatcher.process_update(update)
    return 'ok'


# HANDLERS

# New a dispatcher for bot
dispatcher = Dispatcher(bot=bot, update_queue=None, workers=32)

# Add handler for handling message, there are many kinds of message. For this handler, it particular handle text
# message.
dispatcher.add_handler(MessageHandler(Filters.text, reply_handler))
dispatcher.add_handler(MessageHandler(Filters.reply, reply_handler))
dispatcher.add_handler(MessageHandler(Filters.forwarded, reply_handler))
dispatcher.add_handler(MessageHandler(Filters.video, video_handler))
dispatcher.add_handler(MessageHandler(Filters.contact, contact_handler))
dispatcher.add_handler(MessageHandler(Filters.animation, animation_handler))
dispatcher.add_handler(MessageHandler(Filters.audio, audio_handler))
dispatcher.add_handler(MessageHandler(Filters.document, document_handler))
dispatcher.add_handler(MessageHandler(Filters.game, game_handler))
dispatcher.add_handler(MessageHandler(Filters.invoice, invoice_handler))
dispatcher.add_handler(MessageHandler(Filters.location, location_handler))
dispatcher.add_handler(MessageHandler(Filters.passport_data, passport_data_handler))
dispatcher.add_handler(MessageHandler(Filters.photo, photo_handler))
dispatcher.add_handler(MessageHandler(Filters.private, private_handler))
dispatcher.add_handler(MessageHandler(Filters.sticker, sticker_handler))
dispatcher.add_handler(MessageHandler(Filters.successful_payment, successful_payment_handler))

dispatcher.add_error_handler(error_handler)


if __name__ == "__main__":
    # Running server
    app.run(debug=True)

0 个答案:

没有答案