在python flask中,如何在一个路由中调用两个函数?
我当前正在尝试转换此代码: 这是一个到python的电报机器人代码 我正在使用以下代码: 作为骨架代码 我的app.py如下所示:在python flask中,如何在一个路由中调用两个函数?,python,flask,Python,Flask,我当前正在尝试转换此代码: 这是一个到python的电报机器人代码 我正在使用以下代码: 作为骨架代码 我的app.py如下所示: from flask import Flask, render_template, request, session from PIL import Image from caption import get_question import requests from functools import partial import logging impor
from flask import Flask, render_template, request, session
from PIL import Image
from caption import get_question
import requests
from functools import partial
import logging
import random
from evaluate_chatbot import load_chatbot_model, give_feedback
import string
import os
import json
questions = ''
num_images = 7
model_dir = ''
img_directory = 'data/my_test_images/'
test_data_file = 'data/filenames_list/my_test_filenames.txt'
idx = 0
img_path = ''
shown_img_paths = []
def get_random_pic():
count = 0
global shown_img_paths
with open(test_data_file) as f:
lines = f.readlines()
img_path = random.choice(lines)
while img_path in shown_img_paths:
img_path = random.choice(lines)
count += 1
if count == 30:
return ''
if img_path not in shown_img_paths:
shown_img_paths.append(img_path)
print('image path ', img_path)
return img_directory + img_path.rstrip()
def feedback_and_question_2(reply):
out = give_feedback(encoder, decoder, searcher, voc, reply)
out = "".join([" " + i if not i.startswith("'") and i not in string.punctuation else i for i in out]).strip()
out = out.split('.', 1)[0]
if '?' in out:
out = out.split('?', 1)[0] + '?'
if out == 'what?':
out = 'Nice'
if out == 'no':
out = 'ok'
return(out)
def VQG(response):
img_path = get_random_pic()
image = Image.open(img_path)
image.show()
questions = get_question(img_path)
return questions
# Load Model
checkpoint_iter = 13000
load_model = os.path.join('checkpoints_dialog_model/cornell_model/cornell-movie-dialogs/fine_tunned_cornell', '{}_checkpoint.tar'.format(checkpoint_iter))
encoder, decoder, searcher, voc = load_chatbot_model(load_model)
print("Bot started")
### Routes here ###
app = Flask(__name__)
app.static_folder = 'static'
# Config logging
log_format = '%(levelname)-8s %(message)s'
logfile = os.path.join(model_dir, 'eval.log')
logging.basicConfig(filename= logfile, level=logging.INFO, format=log_format)
logging.getLogger().addHandler(logging.StreamHandler())
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
response = request.args.get('msg')
logging.info(response)
return str(VQG(response))
# @app.route("/get")
# def chatbot():
# response = request.args.get('msg')
# logging.info(response)
# out = feedback_and_question_2(response)
# return out
所以基本上我只能调用一个函数是app.route(“/get”),我需要在不退出函数的情况下并发调用两个函数您可以使用
并发
API并行启动两个线程并同步收集它们的结果
import concurrent.futures
从flask导入jsonify
@app.route(“/get”)
def get_bot_response():
response=request.args.get('msg')
将concurrent.futures.ThreadPoolExecutor(max_workers=2)作为ex:
f1=ex.submit(VG,响应)
f2=例如提交(反馈和问题2,回答)
#等待结果
r1=f1.result()
r2=f2.result()
返回jsonify(ans1=str(r1),ans2=r2)
您确实需要这两个调用同时进行吗?如果是这样的话,你需要使用这里描述的方法,否则你只能进行两次连续调用。任何函数都可以调用任意数量的其他函数。好的,谢谢。我将尝试这种方法。我应该先调用VQG函数来生成1到5个问题,然后调用chatbot函数来获得响应。我是否可以在不退出函数的情况下调用return?flask中是否有类似的函数,如python telegram bot使用的update.message.reply_text()?它在HTML聊天窗口中返回[object object],我知道原因