Skip to content
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

feat: add poc flow for async requests #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pipelines/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
from pipelines.deteccao_alagamento_cameras import * # noqa
from pipelines.exemplo import * # noqa
from pipelines.lgpd import * # noqa
from pipelines.sandbox import * # noqa
from pipelines.stress import * # noqa
from pipelines.templates import * # noqa
2 changes: 2 additions & 0 deletions pipelines/sandbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from pipelines.sandbox.async_requests.flows import * # noqa
Empty file.
24 changes: 24 additions & 0 deletions pipelines/sandbox/async_requests/flows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from prefect import Flow, Parameter
from prefect.run_configs import KubernetesRun
from prefect.storage import GCS
from prefeitura_rio.pipelines_utils.state_handlers import handler_inject_bd_credentials

from pipelines.constants import constants
from pipelines.sandbox.async_requests.tasks import get_random_cat_pictures

with Flow(
name="Sandbox: Test async requests",
state_handlers=[handler_inject_bd_credentials],
) as sandbox__async_requests_flow:
# Parameters
batch_size = Parameter("batch_size", default=50)
n_times = Parameter("n_times", default=500)

get_random_cat_pictures(n_times=n_times, batch_size=batch_size)

sandbox__async_requests_flow.storage = GCS(constants.GCS_FLOWS_BUCKET.value)
sandbox__async_requests_flow.run_config = KubernetesRun(
image=constants.DOCKER_IMAGE.value,
labels=["escritoriodedados"],
)
27 changes: 27 additions & 0 deletions pipelines/sandbox/async_requests/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
import asyncio

from httpx import AsyncClient, AsyncHTTPTransport
from prefect import task
from prefeitura_rio.pipelines_utils.logging import log


@task
def get_random_cat_pictures(n_times: int, batch_size: int):
async def get_single_cat_picture():
transport = AsyncHTTPTransport(retries=3)
async with AsyncClient(transport=transport, timeout=None) as client:
response = await client.get("https://cataas.com/cat")
return response.headers["Content-Type"]

async def main():
log("Starting to fetch cat pictures")
awaitables = [get_single_cat_picture() for _ in range(n_times)]
awaitables = [
awaitables[i : i + batch_size] for i in range(0, len(awaitables), batch_size) # noqa
]
for awaitables_batch in awaitables:
responses = await asyncio.gather(*awaitables_batch)
log(f"Fetched {len(responses)} cat pictures")

asyncio.run(main())
72 changes: 70 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pillow = "^10.1.0"
shapely = "^2.0.2"
h3 = "^3.7.6"
google-cloud-asset = "^3.24.1"
httpx = "^0.27.0"

[tool.poetry.group.dev]
optional = true
Expand Down
Loading