question

ManirathaGoudKunoor-5608 avatar image
0 Votes"
ManirathaGoudKunoor-5608 asked ryanchill commented

Need steps to deploy python flask application and run it from __main__()

I have deployed a flask application on Ubuntu Server on azure. The application seems to be serving the pages but it is not executing the code in the main() function. I require the application to execute the code in main() function too.

Please help me running the application as describe above.

azure-webapps
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered ryanchill commented

Hi @ManirathaGoudKunoor-5608,

To get your app to run __main__(), you can set the startup parameter to the python module containing the function. For example, if your main sub is in main.py, then you will set gunicorn -bind=0.0.0.0 main:app as the startup parameter for your app service.

You can read more about configuring the startup for your Python application on https://docs.microsoft.com/en-us/azure/app-service/configure-language-python#container-startup-process

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am using the command above. But the server is not running the main() function. The print statements in the main() are not getting displayed in the console. My other routes use variables declared in the main() function. When I trigger them I get an internal server error and I can see that the variables declared in the main() are not accessible by the code in the routes.

The variables in the main() are multiprocessing managers .

 if __name__ == '__main__':
     try:
         print("In Main")
         multiprocessing.freeze_support()
         mg = multiprocessing.Manager()
         ns = mg.Namespace()
         ds = mg.Namespace()


0 Votes 0 ·
ryanchill avatar image ryanchill ManirathaGoudKunoor-5608 ·

So, you if the above code snippet is in main.py do you have gunicorn --bind=0.0.0.0 --timeout 600 main:app set as your startup command?

enter-startup-file-for-app-service-in-the-azure-portal.png

Please see https://docs.microsoft.com/en-us/azure/developer/python/tutorial-deploy-app-service-on-linux-04 for more details on how to configure the startup command for your python app.


0 Votes 0 ·