You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR adds support for async operations to the Replicate client.
Namespace operations like `predictions.list` and `models.create` now
have async variants with the `async_` prefix (`predictions.async_list`
and `models.async_create`).
Here's an example of what that looks like in practice:
```python
import replicate
model = await replicate.models.async_get("stability-ai/sdxl")
input = {
"prompt": "A chariot pulled by a team of rainbow unicorns, driven by an astronaut, dramatic lighting",
}
output = await replicate.async_run(f"stability-ai/sdxl:{model.latest_version.id}", input)
```
<details>
<summary>Output</summary>
<img
src="https://github.com/replicate/replicate-python/assets/7659/6927f8b4-5f92-495d-a87c-135f31aa1847"/>
</details>
One of the most common questions I hear is how to run a bunch of
predictions in parallel. The async functionality provided by this PR
makes it really straightforward:
```python
import asyncio
import replicate
# https://replicate.com/stability-ai/sdxl
model_version = "stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b"
prompts = [
f"A chariot pulled by a team of {count} rainbow unicorns"
for count in ["two", "four", "six", "eight"]
]
async with asyncio.TaskGroup() as tg:
tasks = [
tg.create_task(replicate.async_run(model_version, input={"prompt": prompt}))
for prompt in prompts
]
results = await asyncio.gather(*tasks)
print(results)
```
Under the hood, `Client` manages an `httpx.Client` and an
`httpx.AsyncClient`, which handle calls to `_request` and
`_async_request`, respectively. Both are created lazily, so API
consumers using only sync or only async functionality won't be affected
by functionality they aren't using.
Implementation-wise, sync and async variants have separate code paths.
This creates nontrivial amounts of duplication, but its benefits to
clarity and performance justify those costs. For instance, it'd have
been nice if the sync variant were implemented as a blocking call to the
async variant, but that would require starting an event loop, which has
additional overhead and causes problems if done within an existing event
loop.
Alternative to #76Resolves#145Resolves#107Resolves#74
---------
Signed-off-by: Mattt Zmuda <[email protected]>
0 commit comments