|
| 1 | +--- |
| 2 | +title: 'partial' |
| 3 | +tags: 'python' |
| 4 | +date: 'Oct 8, 2025' |
| 5 | +--- |
| 6 | + |
| 7 | +understanding partial() with async/futures |
| 8 | + |
| 9 | +## the basic idea |
| 10 | + |
| 11 | +partial "freezes" args in a fn so you don't have to pass them every time |
| 12 | + |
| 13 | +```py |
| 14 | +from functools import partial |
| 15 | + |
| 16 | +def add(a, b, c): |
| 17 | + return a + b + c |
| 18 | + |
| 19 | +add_5_and_10 = partial(add, 5, 10) |
| 20 | +add_5_and_10(3) # returns 18 (same as add(5, 10, 3)) |
| 21 | +``` |
| 22 | + |
| 23 | +## the problem: fetching from multiple APIs |
| 24 | + |
| 25 | +imagine you need to fetch user data from 3 different API endpoints at the same time |
| 26 | + |
| 27 | +here's the messy way: |
| 28 | + |
| 29 | +```py |
| 30 | +import asyncio |
| 31 | +from functools import partial |
| 32 | +from concurrent.futures import ThreadPoolExecutor |
| 33 | + |
| 34 | +def fetch_data(user_id, api_endpoint, timeout=30, retry=3, api_key="secret"): |
| 35 | + return f"Data from {api_endpoint} for user {user_id}" |
| 36 | + |
| 37 | +async def get_user_data_messy(user_id): |
| 38 | + executor = ThreadPoolExecutor() |
| 39 | + loop = asyncio.get_event_loop() |
| 40 | + |
| 41 | + # repetition |
| 42 | + future1 = loop.run_in_executor( |
| 43 | + executor, |
| 44 | + lambda: fetch_data(user_id, "profile", 30, 3, "secret") |
| 45 | + ) |
| 46 | + future2 = loop.run_in_executor( |
| 47 | + executor, |
| 48 | + lambda: fetch_data(user_id, "orders", 30, 3, "secret") |
| 49 | + ) |
| 50 | + future3 = loop.run_in_executor( |
| 51 | + executor, |
| 52 | + lambda: fetch_data(user_id, "reviews", 30, 3, "secret") |
| 53 | + ) |
| 54 | + |
| 55 | + results = await asyncio.gather(future1, future2, future3) |
| 56 | + return results |
| 57 | +``` |
| 58 | + |
| 59 | +the clean way with partial: |
| 60 | + |
| 61 | +```py |
| 62 | +async def get_user_data_clean(user_id): |
| 63 | + executor = ThreadPoolExecutor() |
| 64 | + loop = asyncio.get_event_loop() |
| 65 | + |
| 66 | + # common way |
| 67 | + fetcher = partial( |
| 68 | + fetch_data, |
| 69 | + user_id=user_id, |
| 70 | + timeout=30, |
| 71 | + retry=3, |
| 72 | + api_key="secret" |
| 73 | + ) |
| 74 | + |
| 75 | + endpoints = ["profile", "orders", "reviews"] |
| 76 | + |
| 77 | + futures = [ |
| 78 | + loop.run_in_executor(executor, partial(fetcher, api_endpoint=ep)) |
| 79 | + for ep in endpoints |
| 80 | + ] |
| 81 | + |
| 82 | + results = await asyncio.gather(*futures) |
| 83 | + return results |
| 84 | +``` |
| 85 | + |
| 86 | +## why the double partial |
| 87 | + |
| 88 | +```py |
| 89 | +loop.run_in_executor(executor, partial(fetcher, api_endpoint=ep)) |
| 90 | +``` |
| 91 | + |
| 92 | +here's what's actually happening: |
| 93 | + |
| 94 | +```py |
| 95 | +# first partial: lock in the common stuff |
| 96 | +fetcher = partial(fetch_data, user_id=user_id, timeout=30, retry=3, api_key="secret") |
| 97 | + |
| 98 | +# second partial: add the specific endpoint |
| 99 | +profile_fetcher = partial(fetcher, api_endpoint="profile") |
| 100 | + |
| 101 | +# now profile_fetcher() is a zero-argument callable |
| 102 | +# calling it is the same as: fetch_data(user_id, "profile", 30, 3, "secret") |
| 103 | +``` |
| 104 | + |
| 105 | +## seeing it run |
| 106 | + |
| 107 | +```py |
| 108 | +import time |
| 109 | + |
| 110 | +def fetch_data(user_id, api_endpoint, timeout=30, retry=3, api_key="secret"): |
| 111 | + time.sleep(1) # pretend this is an API call |
| 112 | + return f"Data from {api_endpoint} for user {user_id}" |
| 113 | + |
| 114 | +async def main(): |
| 115 | + start = time.time() |
| 116 | + results = await get_user_data_clean(12345) |
| 117 | + print(f"completed in {time.time() - start:.2f}s") |
| 118 | + print(results) |
| 119 | + # completed in 1.01s (all 3 APIs ran at the same time) |
| 120 | + # ['Data from profile for user 12345', |
| 121 | + # 'Data from orders for user 12345', |
| 122 | + # 'Data from reviews for user 12345'] |
| 123 | + |
| 124 | +asyncio.run(main()) |
| 125 | +``` |
0 commit comments