From c367450e9839edd9995fbc7dacf8cef32fa859c8 Mon Sep 17 00:00:00 2001 From: devdudumuniz <82589615+devdudumuniz@users.noreply.github.com> Date: Tue, 1 Sep 2026 07:12:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20Fix=20blocking=20file=20I/O?= =?UTF-8?q?=20in=20async=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced synchronous `open` and `f.write` with `anyio.open_file` and `await f.write` inside `DadosGov.download` to prevent blocking the async event loop during chunk downloads. --- pysus/api/dadosgov/client.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pysus/api/dadosgov/client.py b/pysus/api/dadosgov/client.py index c7cf8088..2889d39e 100644 --- a/pysus/api/dadosgov/client.py +++ b/pysus/api/dadosgov/client.py @@ -4,6 +4,8 @@ import pathlib from collections.abc import Callable + +import anyio from datetime import datetime from typing import TYPE_CHECKING, Annotated, Any, Optional @@ -295,9 +297,9 @@ async def download( response.raise_for_status() total = int(response.headers.get("Content-Length", 0)) downloaded = 0 - with open(output, "wb") as f: + async with await anyio.open_file(output, "wb") as f: async for chunk in response.aiter_bytes(): - f.write(chunk) + await f.write(chunk) downloaded += len(chunk) if callback: callback(downloaded, total)