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

问题描述 投票:0回答:1

我的代码在本地主机上运行良好,但部署在 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 上的运行效果一样好

python azure flask python-multithreading
1个回答
0
投票

我对您的代码做了一些更改并尝试在我的环境中执行。我能够成功部署到 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)

本地环境执行成功:

enter image description here

部署成功如图:

enter image description here

enter image description here

部署完成后,前往应用服务的

Advanced tools
下的
Development tools
,即可查看部署情况,如图。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.