Python Flask App Service with threads is not working

HTH-3183 1 Reputation point

Hello, I have a Flask API running in App Services in a container and I want to return 200 as soon as possible while a thread is doing some logic (let's say, send an email). It works locally, I receive the email, but when I deploy it in azure, I get 200 but no email.

In general, the code is like:

def main():
    data = request.get_json()    
    t = Thread(target=send_mail, args=(data, )
    return "OK"

Any idea? Is it because is a container? because Azure?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,830 questions
{count} votes

2 answers

Sort by: Most helpful
  1. HTH-3183 1 Reputation point

    I cannot disclose much of my code, the email was an example. I'm trying to access shotgun API and it seems that shotgun is the problem. No errors to show in console btw.

    for example:

    @flaskblueprint.route('/api/my_endpoint', methods=['POST'])
    def my_endpoint():
        print(">>> myendpoint got a request")
        example_str= request.get_json()
        t = Thread(target=do_something, args=(example_str, ))
        return rmsg.resp_msg(200, "Email sent")
    def do_something(example_str):
        user = sg.find_one('HumanUser',[['id','is', xxx]],['login'])
        print('PRINT THIS FROM SHOTGUN {}'.format(user))

    That code would shot this in console when docker-compose

    spawned uWSGI master process (pid: 1)
    my-api | spawned uWSGI worker 1 (pid: 7, cores: 1)
    my-api | spawned uWSGI worker 2 (pid: 8, cores: 1)
    my-api | >>> myendpoint got a request
    my-api | Hi
    my-api | pid: 7|app: 0|req: 1/1] () {34 vars in 525 bytes} [Tue Dec 22 07:15:26 2020] POST /api/my_endpoint => generated 40 bytes in 5 msecs (HTTP/1.1 200) 2 headers in 71 bytes (1 switches on core 0)

    I did it in a python 3.8 azure web app, and it works. If I use a web app using containers or docker-compose locally, it won't and it doesn't give me any error.

  2. Erik Palenčík 0 Reputation points

    2022, still having similar problem.

    This simple Flask code works perfectly on my localhost with but not when deployed on azure. On azure when i hit "/wait" whole server freezes. On localhost when I hit wait I can still access "/" I have Basic B3 100 ACU / 4vCPU / 7GM ram. Python 3.11 and async flask.

    app = Flask("Infinity", static_url_path='')
    async def index():    
           return "Test"
    async def wait():    
        return "Test"