Using async for concurrent work

Introduction to FastAPI

Matt Eckerle

Software and Data Engineering Leader

Why use async? Concurrent Burgers!

Sequential Burgers

Customers in line for burgers prepared and served in serial

Concurrent Burgers

Customers in line for burgers prepared and served concurrently

1 https://fastapi.tiangolo.com/async/
Introduction to FastAPI

async in practice

Sequential Burgers

Defining a function to get burgers

# This is not asynchronous
def get_sequential_burgers(number: int):
    # Do some sequential stuff
    return burgers

Calling the function sequentially

burgers = get_burgers(2)

Concurrent Burgers

Defining a function to get burgers

async def get_burgers(number: int):
    # Do some asynchronous stuff
    return burgers

Calling the function asynchronously

burgers = await get_burgers(2)
Introduction to FastAPI

FastAPI with async

If we can:

results = await some_library()

Then use async def:

@app.get('/')
async def read_results():
    results = await some_library()
    return results

Note Only use await inside of functions created with async def

Introduction to FastAPI

When to use async

Use async

If our application doesn't have to communicate with anything else and wait for it to respond

Examples

  • Audio or image processing
  • Computer vision
  • Machine Learning
  • Deep Learning

Don't use async

If our application has to communicate with:

  • File system
  • Database
  • Another server

If we aren't sure

Introduction to FastAPI

Let's practice!

Introduction to FastAPI

Preparing Video For Download...