Crux Intelligence
Published in

Crux Intelligence

Concurrency with FastAPI

Concurrency with FastAPI

Understanding Concurrency and Parallelism

Concurrency

Parallelism

Introduction to Async IO

  • Declare the first path functions as coroutines via async def wherever appropriate. If we do this wrong, FastAPI will still be able to handle it, we just won’t get the performance benefits.
  • Declare particular points as awaitable via the await keyword within the coroutines.
  1. Setup and Installation
  2. Add a Sync EntryPoint
  3. Add Middleware
  4. Start the Application
  5. Test the Application
  6. Add an Async EntryPoint
  7. Compare the Performance

1. Setup and Installation

create-project-folder
start pipenv shell
install-dependencies
  • Pipfile contains all the names of the dependencies we just installed.
  • Pipfile.lock is intended to specify, based on the dependencies present in Pipfile, which specific version of those should be used, avoiding the risks of automatically upgrading dependencies that depend upon each other and breaking your project dependency tree.

2. Add a Sync EntryPoint

get_universities
get_all_universities_for_country
University Model

3. Add middleware

add_process_time_header

4. Start the Application

main.py
python main.py
swagger-with-sync-API

5. Test the application

sync-response

6. Add an Async EntryPoint

get_universities_async
get_all_universities_for_country_async

7. Compare the Performance

sample-FastAPI-application
async-API

Conclusion

References & Useful Readings

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store