'With similar code, why Fastapi behave differently to Flask? Fastapi run the whole code again when using multiprocessing but not in Flask

I am trying to compare using webhook with flask and using fastapi, both have similar code but why they come up with different output? (Fastapi run the whole code for another extra time when comparing to Flask)

#webhook_flask.py    
import time
from flask import Flask, request, abort
import multiprocessing as mp


def printmessage(job):
    
    time.sleep(5) 
    print(job)    


print(__name__)

if __name__ == '__main__':
    app = Flask(__name__)
    @app.route('/webhook', methods=['POST'])
    def webhook():     
            
        print("WEBHOOK RECEIVED")  

        job="doctor"  
        p =mp.Process(target=printmessage, args=[job])
        p.start() 
        print('done')   
        return 'WEBHOOK RECEIVED' 

if __name__ == '__main__':
    print("PROGRAM LAUNCH...")
    print("WEBHOOK RECEIVE READY...")
    app.run() 
 

OUTPUT:

__main__
PROGRAM LAUNCH...
WEBHOOK RECEIVE READY...
 * Serving Flask app 'webhook_flask' (lazy loading)
 * 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
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
WEBHOOK RECEIVED
done
127.0.0.1 - - [06/May/2022 01:05:01] "POST /webhook HTTP/1.1" 200 -
__mp_main__
doctor

Please note that for flask, it only run __mp_main__ when webhook is received.

For Fastapi, with similar code:

#webhook_fastapi.py
import time
from fastapi import Request, FastAPI
import multiprocessing as mp
import uvicorn

def printmessage(job):    
    time.sleep(5) 
    print(job)
   
print(__name__)
 
if __name__ == 'webhook_fastapi':  
    app = FastAPI()  
    @app.post("/webhook")
    async def webhook(request : Request):              
        print("WEBHOOK RECEIVED")  
        job="doctor"  
        p =mp.Process(target=printmessage, args=[job])
        p.start() 
        print('done')   
        return 'WEBHOOK RECEIVED'

if __name__ == '__main__':     
    print("PROGRAM LAUNCH...")
    print("WEBHOOK RECEIVE READY...") 
    uvicorn.run("webhook_fastapi:app", reload=False)  

OUTPUT

__main__
PROGRAM LAUNCH...
WEBHOOK RECEIVE READY...
webhook_fastapi
INFO:     Started server process [94852]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
WEBHOOK RECEIVED
done
INFO:     127.0.0.1:49360 - "POST /webhook HTTP/1.1" 200 OK
__mp_main__
webhook_fastapi
doctor

For Fastapi, once the webhook is received, the process of __mp_main__ will run the code just like Flask, but why webhook_fastapi will run again? Is there some error in my code or that is just how Fastapi behave? As I am planning to move from Flask to Fastapi but this issue has stopping me from doing it. Any solution to make the Fastapi only run in the identity of __mp_main__ but not webhook_fastapi when the new process is created once the webhook is received?

Besides, I try to add this line of code inside the function printmessage() :

print(f'inside print msg process {__name__}')

For Flask, the output is inside print msg process __mp_main__ but for Fastapi the output is inside print msg process webhook_fastapi, how is that possible? It appears that Fastapi did not run the function with the new process at all?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source