-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_batch.py
More file actions
54 lines (41 loc) · 1.54 KB
/
async_batch.py
File metadata and controls
54 lines (41 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Async batch processing example.
Demonstrates submitting multiple jobs concurrently with AsyncDeapiClient.
Usage:
export DEAPI_API_KEY="sk-your-api-key"
python examples/async_batch.py
"""
import asyncio
from deapi import AsyncDeapiClient
async def main() -> None:
async with AsyncDeapiClient() as client:
# Check balance before submitting
balance = await client.balance()
print(f"Account balance: ${balance.balance}")
# Submit multiple image generation jobs concurrently
prompts = [
"a mountain landscape at golden hour",
"an ocean sunset with dramatic clouds",
"a misty forest path in autumn",
"a futuristic city skyline at night",
]
print(f"\nSubmitting {len(prompts)} jobs concurrently...")
jobs = await asyncio.gather(*[
client.images.generate(
prompt=prompt,
model="Flux1schnell",
width=1024,
height=1024,
seed=i + 1,
)
for i, prompt in enumerate(prompts)
])
for job in jobs:
print(f" Submitted: {job.request_id}")
# Wait for all results concurrently
print("\nWaiting for results...")
results = await asyncio.gather(*[job.wait() for job in jobs])
for i, result in enumerate(results):
print(f"\n Job {i + 1}: {prompts[i]}")
print(f" Status: {result.status}")
print(f" URL: {result.result_url}")
asyncio.run(main())