Skip to content

Stream and Schedule Downloads

Client.iter_download() yields ordered bytes and does not materialize the full file. It accepts normalized media, compatible raw media or a miniproto file ID. The stream rejects resume and multi_session; use an explicit offset for a ranged continuation.

from pathlib import Path
from miniproto import Client
async def stream_to_file(client: Client, file_id: str, destination: Path) -> None:
with destination.open("wb") as output:
async for chunk in client.iter_download(file_id, concurrency=2, max_in_flight_bytes=4 * 1024 * 1024):
output.write(chunk)

The stream closes outstanding part tasks when you close the generator or cancel its caller. concurrency sets the bounded part-request window. It does not guarantee throughput. Choose it with the byte budget, server flood waits, local memory and connection health in mind.

For separate known media objects, schedule top-level downloads concurrently and let the client-level media scheduler apply its configured per-DC budgets:

import asyncio
from pathlib import Path
from miniproto import Client
async def download_pair(client: Client, first: object, second: object) -> None:
async with asyncio.TaskGroup() as tasks:
tasks.create_task(client.download_media(first, Path("first.bin"), concurrency=2))
tasks.create_task(client.download_media(second, Path("second.bin"), concurrency=2))

download_media() can write to memory, a path or a caller-owned destination. Set multi_session=True to enable multiple sessions for complete, known-size bot downloads with sibling storage. Unsupported cases fall back to one session. See Media Primitives for range alignment, integrity checks, CDN redirects, destinations, progress callbacks, file IDs and cancellation behavior.