Skip to content

Add async_wait method to Prediction class #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions replicate/prediction.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import re
import time
from dataclasses import dataclass
Expand Down Expand Up @@ -127,6 +128,14 @@ def wait(self) -> None:
time.sleep(self._client.poll_interval)
self.reload()

async def async_wait(self) -> None:
"""
Wait for prediction to finish.
"""
while self.status not in ["succeeded", "failed", "canceled"]:
await asyncio.sleep(self._client.poll_interval)
await self.async_reload()

def stream(self) -> Optional[Iterator["ServerSentEvent"]]:
"""
Stream the prediction output.
Expand Down Expand Up @@ -164,6 +173,15 @@ def reload(self) -> None:
for name, value in updated.dict().items():
setattr(self, name, value)

async def async_reload(self) -> None:
"""
Load this prediction from the server.
"""

updated = await self._client.predictions.async_get(self.id)
for name, value in updated.dict().items():
setattr(self, name, value)

def output_iterator(self) -> Iterator[Any]:
"""
Return an iterator of the prediction output.
Expand Down
4 changes: 2 additions & 2 deletions replicate/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ async def async_run(
)

if not version and (owner and name and version_id):
version = Versions(client, model=(owner, name)).get(version_id)
version = await Versions(client, model=(owner, name)).async_get(version_id)

if version and (iterator := _make_output_iterator(version, prediction)):
return iterator

prediction.wait()
await prediction.async_wait()

if prediction.status == "failed":
raise ModelError(prediction.error)
Expand Down