Flask 2.0 vs FastAPI in REST API development Tetsuya (Jesse) Hirata @JesseTetsuya ———————————————————————————————————————————————————————————————————————————————— Software Engineer at an EdTech company based in Tokyo. I mostly work in both data science and engineering. PyCon 2021 talk at !
Background @JesseTetsuya - There are many kinds of web application frameworks. - REST API developments for micro service architecture and AI / ML projects are in demand.
Approaches Evaluation based on the four criteria between Flask 2.0 and FastAPI for REST API development 1. Variety of functions and extensions 2. Performance(Speed and Stability) 3. Flexibility of REST API Architecture 4. Learning Cost @JesseTetsuya
Follow up: Flask 2.0 Flask has major version up in May 2021 - Nested Blueprint - Async/Await - Type Hinting - 15x speed up of multipart/form-body - Routing shortcut - etc. @JesseTetsuya 'MBTLSFMFTFOPUFIUUQTqBTLQBMMFUTQSPKFDUTDPNFOYDIBOHFT
Answers 1. Variety of functions and extensions 2. Performance(Speed and Stability) 3. Flexibility of REST API Architecture 4. Learning Cost @JesseTetsuya
@JesseTetsuya 'BTU"1*&YUFOTJPOMJTUIUUQTHJUIVCDPNNKIFBBXFTPNFGBTUBQJ 'MBTL&YUFOTJPOIUUQTqBTLQBMMFUTQSPKFDUTDPNFOYFYUFOTJPOEFW 1. Variety of functions and extensions
Pydantic: - Input data validation and settings management using python type annotations. -> Pydantic have good match with data class, type hint, mypy, and OpenAPI. OpenAPI: - Swagger Editor - API document editor which has preview function - Swagger UI - Browsing the api documentation based on schemas written on yaml or json - OpenAPI Generator - Generate automatically the client server stub code based on the yaml or json -> Sphinx and JSON schema is recommended, specially in AI/ML projects. Request/Application Context: - Request context keeps track of the request-level data (URL, HTTP method, headers, request data, session info) and allows us to access them by using proxies such as request and session. - Application context keeps track of the application-level data (configuration variables, logger, database connection) allows us to access them by using proxies such as current_app and g. -> Next Slide @JesseTetsuya 1. Variety of functions and extensions
UZQFJHOPSF qBTLTSDqBTLHMPCBMQZ - The right picture means that a unique werkzeug.local.LocalStack object has multiple unique threads - The data need to be stored in a way that only one worker(thread) can retrieve back, which means the data get back their own specific data that’s unique to each worker(thread).
@JesseTetsuya 'BTU"1*&YUFOTJPOMJTUIUUQTHJUIVCDPNNKIFBBXFTPNFGBTUBQJ 'MBTL&YUFOTJPOIUUQTqBTLQBMMFUTQSPKFDUTDPNFOYFYUFOTJPOEFW 1. Variety of functions and extensions
Answers 1. Variety of functions and extension 2. Performance(Speed and Stability) 3. Flexibility of REST API Architecture 4. Learning Cost @JesseTetsuya
4QFFE 4UBCJMJUZ 3FRVFTUT
%VSBUJPOT
T
T -BUFODJFTNT
T
T
T
T
T
T #ZUFT*O
#ZUFT0VU
4VDDFTT 4UBUVT$PEFT &SSPS4FU 3FRVFTUT
%VSBUJPOT
T
NT -BUFODJFTNT
NT
NT
NT
NT
NT
NT #ZUFT*O
#ZUFT0VU
4VDDFTT 4UBUVT$PEFT &SSPS4FU @JesseTetsuya 2. Performance(Speed and Stability)
@JesseTetsuya Performance test is very difficult only by just comparing frameworks… - web server (nginx/apache) - interfaces (WSGI/ASGI) - application server (uwsgi, gunicorn, uvicorn) - libraries (asyncio(explicit) / gevent(implicit)) - application code(sync / async) - language (CPython, Cython…) - architecture from tier one to tier three… - private cloud services(AWS/GCP/Azure…) 2. Performance(Speed and Stability)
4QFFE 4UBCJMJUZ 3FRVFTUT
%VSBUJPOT
T
T -BUFODJFTNT
T
T
T
T
T
T #ZUFT*O
#ZUFT0VU
4VDDFTT 4UBUVT$PEFT &SSPS4FU 3FRVFTUT
%VSBUJPOT
T
NT -BUFODJFTNT
NT
NT
NT
NT
NT
NT #ZUFT*O
#ZUFT0VU
4VDDFTT 4UBUVT$PEFT &SSPS4FU @JesseTetsuya 2. Performance(Speed and Stability)
Answers 1. Variety of Functions / Extension / Tools 2. Performance(Speed and Stability) of sync and async 3. Flexibility of REST API Architecture 4. Learning Cost @JesseTetsuya
@JesseTetsuya 3. Flexibility of REST API Architecture 1BUUFSOTGPS'MBTLIUUQTqBTLQBMMFUTQSPKFDUTDPNFOYQBUUFSOTJOEFYIUNM #JHHFS"QQMJDBUJPOT.VMUJQMF'JMFTIUUQTGBTUBQJUJBOHPMPDPNUVUPSJBMCJHHFSBQQMJDBUJPOT
Answers 1. Variety of functions and extension 2. Performance(Speed and Stability) 3. Flexibility of REST API Architecture 4. Learning Cost @JesseTetsuya
@JesseTetsuya OFF TOPIC Learning Strategies 1. Recognize what you do not know and categorize the type of knowledge: Capture the whole picture by reviewing movies, personally recommend python conference archives. 2. Acquire Declarative Knowledge: Google ambiguous terms, link word with meaning, and make relations between a term and a term. Indexes in this previous slides can be useful. 3. Acquire Procedural Knowledge: Focus on writing the code and develop a small application based on tutorial materials or primers.
@JesseTetsuya 4. Learning Cost Learning materials for engineers without Async experience 1. Miguel Grinberg Asynchronous Python for the Complete Beginner PyCon 2017 (https://youtu.be/iG6fr81xHKA) 2. An introduction to ASGI, Asynchronous Server Gateway Interface - Philip Jones - PyLondinium19(https://youtu.be/t3gCK9QqXWU) 3. asyncio — Asynchronous I/O (https://docs.python.org/3/library/ asyncio.html)
@JesseTetsuya 4. Learning Cost Learning materials for engineers without FastAPI experience 1. Modern Python through FastAPI and friends. Sebastián Ramírez. (https://youtu.be/37CcB2GBdlY) 2. Tutorial - User Guide - Intro (https://fastapi.tiangolo.com/tutorial/#tutorial-user-guide-intro) 3. Talks # 8: Sebastián Ramírez; Build a machine learning API from scratch with FastAPI (https://youtu.be/1zMQBe0l1bM)
@JesseTetsuya 4. Learning Cost Learning materials for engineers without Flask experience 1. Armin Ronacher, "Flask for Fun and Profit", PyBay2016 (https://youtu.be/1ByQhAM5c1I) 2. CS50 2020 - Lecture 9 - Flask, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. (https://www.youtube.com/watch?v=x_c8pTW8ZUc) 3. Tutorial: (https://flask.palletsprojects.com/en/2.0.x/tutorial/index.html)
@JesseTetsuya The Future - ASGI is not PEP yet. - There are still many unknown practices of architecture patterns including anti-patterns. The amount of experiences and practices can influence framework selection. - Japanese books of Flask and FastAPI are very rare for us to find them in bookstore. This influences the number of learners and the community size.
Tetsuya (Jesse) Hirata @JesseTetsuya ———————————————————————————————————————————————————————————————————————————————— Software Engineer at an EdTech company based in Tokyo. I mostly work in both data science and engineering. If you have an interest in the EdTech domain or in what our team is doing, reach out to me !!