Python 烧瓶/遥控机器人:机器人可以';I don’我不能一边上班一边上班
各位。我需要telebot来接收邮件和回复。我决定用烧瓶。 我有密码:Python 烧瓶/遥控机器人:机器人可以';I don’我不能一边上班一边上班,python,flask,telegram,Python,Flask,Telegram,各位。我需要telebot来接收邮件和回复。我决定用烧瓶。 我有密码: import telebot from flask import Flask, request bot = telebot.TeleBot('dddd') print("1") app = Flask(__name__) print("2") @bot.message_handler(commands=['start']) def start_message(message):
import telebot
from flask import Flask, request
bot = telebot.TeleBot('dddd')
print("1")
app = Flask(__name__)
print("2")
@bot.message_handler(commands=['start'])
def start_message(message):
print("5")
bot.send_message(message.chat.id, message.chat.id)
bot.send_message(message.chat.id, message.chat.username)
print("3")
@app.route('/', methods=['POST'])
def result():
dt = request.args.to_dict(flat=False)
print("6")
for i in dt["chatIds"]:
print(i)
bot.send_message(i, "tema loh")
return "0"
print("4")
bot.polling()
当我启动烧瓶时,我看到:
F:\Desktop\backend\bot_14_11>flask run
* Serving Flask app "bot.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
机器人正在工作,但我无法向本地主机发送帖子。
我按ctrl+c,看到:
- 正在运行(按CTRL+C退出)
flask run
flask时,它将尝试从您的模块导入app
对象。但是,导入将永远不会完成,因为bot.polling()
将永远阻塞。然后,如果按ctrl+c,bot将停止运行,从而允许Flask导入并运行Flask应用程序。正如您所观察到的,bot不会与Flask同时运行
您可能可以在单独的线程中运行bot,但是您必须小心线程的使用
导入线程
... # 除了最后一行“bot.polling”之外,所有代码都保持原样。。。
t=threading.Thread(target=bot.polling,daemon=True)
t、 start()#改为在线程中启动bot
然而,线程是它自己的主题,在尝试之前,您应该非常熟悉它。您还应该考虑使用的库/代码是否是线程安全的。
相反,为了避免线程可能增加的复杂性,我建议将bot和Flask应用程序拆分为单独运行的应用程序。必要时,使用某种其他机制在它们之间进行通信。例如,为每个应用程序运行一个单独的python进程。在两个不同的线程上运行
bot
&app
。请命名一个库或给出提示?