Asynchronous usage
All of the interfaces in Modal are available in both asynchronous and blocking
versions. The async versions are available for import from the modal.aio
module, where classes are prefixed with the Aio
prefix. E.g., instead of
modal.Stub
you can use modal.AioStub
to get asynchronous interfaces on most
methods, intended to be used with Python’s asyncio
library.
import asyncio
import modal.aio
stub = modal.aio.AioStub()
@stub.function
async def myfunc():
...
# some kind of async entrypoint method is needed since Python doesn't allow async code in module scope
async def main():
async with stub.run():
# executes 100 calls to myfunc in parallel
await asyncio.gather(*[myfunc() for i in range(100)])
if __name__ == "__main__":
asyncio.run(main())
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.