Last active
March 6, 2025 00:34
-
-
Save WhiteBlackGoose/c9a57bfc05c69af3a57ee7b164322f08 to your computer and use it in GitHub Desktop.
Async downloader experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gleam/otp/task | |
import gleam/http/request | |
import gleam/int | |
import gleam/string | |
import gleam/list | |
import gleam/io | |
import gleam/httpc | |
import gleam/result | |
fn download(url: String) -> Result(String, String) { | |
use request <- result.try(result.map_error(request.to(url), fn(_) { "Bad request" })) | |
let res = httpc.send(request) | |
use res <- result.try(result.map_error(res, fn(_) { "Http error" })) | |
case string.length (res.body) { | |
25 -> Ok(res.body) | |
_ -> Error("Oh no") | |
} | |
} | |
pub fn main() { | |
let my_webpage = censored out :P | |
let count = 2000 | |
let succ = list.range(1, count) | |
|> list.map(fn(_) { my_webpage }) | |
|> list.map(fn(w) { task.async (fn() { download(w) }) }) | |
|> task.try_await_all (10000) | |
|> list.count (fn(e) { | |
case e { | |
Ok(Ok(_)) -> True | |
_ -> False | |
} | |
}) | |
io.println ("Count: " <> int.to_string (count)) | |
io.println ("Actual: " <> int.to_string (succ)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from typing import Iterable | |
import aiohttp | |
from result import Result, Ok, Err | |
async def download(urls: Iterable[str]) -> list[str]: | |
async with aiohttp.ClientSession() as session: | |
async def download_one(u: str) -> Result[str, Exception | str]: | |
try: | |
async with session.get(u) as resp: | |
if resp.status != 200: | |
return Err(f"HTTP error: {resp.status}") | |
return Ok(await resp.text()) | |
except Exception as e: | |
return Err(e) | |
res = await asyncio.gather(*map(download_one, urls)) | |
filtered: list[str] = [] | |
for r in res: | |
match r: | |
case Err(e): | |
print(f"Error: {e}") | |
pass | |
case Ok(text): | |
filtered.append(text) | |
return filtered | |
async def main(): | |
count = 2000 | |
my_webpage = censored out :P | |
pages = await download(map(lambda _: my_webpage, range(count))) | |
assert len(pages) == count, len(pages) | |
for page in pages: | |
assert len(page) == 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment