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 my_modal_funcion.call("hello")
you can use
my_modal_function.call.aio("hello")
to get an asynchronous coroutine response,
intended to be used with Python’s asyncio
library.
import asyncio
stub = modal.Stub()
@stub.function()
async def myfunc():
...
@stub.local_entrypoint()
async def main():
# executes 100 remote calls to myfunc in parallel
await asyncio.gather(*[myfunc.call.aio() 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.