Python Flask应用程序未在Azure上运行线程

ktca8awb  于 2023-10-22  发布在  Python
关注(0)|答案(1)|浏览(96)

我的代码在localhost上运行良好,但在Azure上部署时线程无法正常工作。代码看起来像这样:

from flask import Flask, request
from side import querying, send_message
import json
from threading import Thread

app = Flask(__name__)

@app.route('/chat', methods=['POST'])
def chat():
    body = json.loads(request.data.decode())['body']
    category = json.loads(request.data.decode())['category']
    phone = json.loads(request.data.decode())['phone']

    def do_work(body, category, phone):
        answer = querying(body, category)
        send_message(phone, answer)

    thread = Thread(target=do_work, kwargs={'body': body, "category":category, "phone":phone})
    thread.start()
    return 'started'

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, port=5000)

我希望它在Azure上的工作效果和在localhost上一样好

0sgqnhkj

0sgqnhkj1#

我对你的代码做了一点修改,并试图在我的环境中执行。成功部署到Azure。

from  flask  import  Flask, request
from  side.querying  import  querying
from  side.send_message  import  send_message
import  json
from  threading  import  Thread
app  =  Flask(__name__)
@app.route('/chat', methods=['POST'])
def  chat():
body  =  json.loads(request.data.decode())['body']
category  =  json.loads(request.data.decode())['category']
phone  =  json.loads(request.data.decode())['phone']
def  do_work(body, category, phone):
answer  =  querying(body, category)
send_message(phone, answer)
thread  =  Thread(target=do_work, kwargs={'body': body, "category":category, "phone":phone})
thread.start()
return  "started"
if  __name__  ==  '__main__':
app.run(host='0.0.0.0', debug=False, port=5000)

在本地环境中成功执行:

已成功部署,如下所示:

部署完成后,转到应用服务Development tools下的Advanced tools,您可以查看部署,如图所示。

更新:我可以在appservice post部署的日志流中查看send_message。

相关问题