Asynchronous API usage

All of the interfaces in Modal are available in both asynchronous and blocking versions. The async interface can be accessed by calling suffixing .aio on the blocking functions/methods in the Modal API.

E.g., instead of"hello") you can use"hello") to get an asynchronous coroutine response, intended to be used with Python’s asyncio library.

import asyncio

stub = modal.Stub()

async def myfunc():

async def main():
    # executes 100 remote calls to myfunc in parallel
    await asyncio.gather(*[ for i in range(100)])

If you are comfortable with asynchronous programming, you can use it to create arbitrary parallel execution patterns, with the added benefit that any Modal functions will be executed remotely.

Asyncronous functions

Regardless if you use an async runtime (like asyncio) in your usage of Modal itself, you are free to define your stub.function-decorated function bodies either async or blocking. Both kinds of definitions will work for remote Modal function calls from both blocking and async contexts.