|
| 1 | +# Dual Pool Support |
| 2 | + |
| 3 | +This guide covers erlang_python's dual pool architecture for separating CPU-bound and I/O-bound Python operations. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +erlang_python provides two separate pools of Python contexts: |
| 8 | + |
| 9 | +| Pool | Purpose | Default Size | Use Case | |
| 10 | +|------|---------|--------------|----------| |
| 11 | +| `default` | Quick CPU-bound operations | Number of schedulers | Math, string processing, data transformation | |
| 12 | +| `io` | Slow I/O-bound operations | 10 | HTTP requests, database queries, file I/O | |
| 13 | + |
| 14 | +This separation prevents slow I/O operations from blocking quick CPU operations. |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +``` |
| 19 | +┌──────────────────────────────────────────────────────────────────┐ |
| 20 | +│ py:call/3,4,5 │ |
| 21 | +│ │ │ |
| 22 | +│ ▼ │ |
| 23 | +│ ┌─────────────────┐ │ |
| 24 | +│ │ lookup_pool() │ │ |
| 25 | +│ │ (registration) │ │ |
| 26 | +│ └────────┬────────┘ │ |
| 27 | +│ │ │ |
| 28 | +│ ┌──────────────┴──────────────┐ │ |
| 29 | +│ ▼ ▼ │ |
| 30 | +│ ┌────────────────┐ ┌────────────────┐ │ |
| 31 | +│ │ default pool │ │ io pool │ │ |
| 32 | +│ │ (N contexts) │ │ (10 contexts) │ │ |
| 33 | +│ └────────────────┘ └────────────────┘ │ |
| 34 | +│ │ │ │ |
| 35 | +│ ┌────────┴────────┐ ┌────────┴────────┐ │ |
| 36 | +│ ▼ ▼ ▼ ▼ ▼ ▼ │ |
| 37 | +│ Ctx1 Ctx2 CtxN Ctx1 Ctx2 Ctx10 │ |
| 38 | +│ (math) (json) (...) (http) (db) (...) │ |
| 39 | +└──────────────────────────────────────────────────────────────────┘ |
| 40 | +``` |
| 41 | + |
| 42 | +## Basic Usage |
| 43 | + |
| 44 | +### Explicit Pool Selection |
| 45 | + |
| 46 | +Specify the pool directly in the call: |
| 47 | + |
| 48 | +```erlang |
| 49 | +%% Use default pool (quick operations) |
| 50 | +{ok, 4.0} = py:call(default, math, sqrt, [16]). |
| 51 | + |
| 52 | +%% Use io pool (slow operations) |
| 53 | +{ok, Response} = py:call(io, requests, get, [Url]). |
| 54 | + |
| 55 | +%% With keyword arguments |
| 56 | +{ok, Data} = py:call(io, requests, get, [Url], #{timeout => 30}). |
| 57 | +``` |
| 58 | + |
| 59 | +### Registration-Based Routing |
| 60 | + |
| 61 | +Register modules or specific functions to automatically route to a specific pool: |
| 62 | + |
| 63 | +```erlang |
| 64 | +%% Register entire module to io pool (all functions in module) |
| 65 | +ok = py:register_pool(io, requests). |
| 66 | +ok = py:register_pool(io, aiohttp). |
| 67 | +ok = py:register_pool(io, psycopg2). |
| 68 | + |
| 69 | +%% Register specific module.function to io pool |
| 70 | +ok = py:register_pool(io, {urllib, urlopen}). %% Only urllib.urlopen |
| 71 | +ok = py:register_pool(io, {httpx, 'get'}). %% Only httpx.get |
| 72 | +ok = py:register_pool(io, {db, query}). %% Only db.query |
| 73 | + |
| 74 | +%% Now calls route automatically - no code changes needed |
| 75 | +{ok, 4.0} = py:call(math, sqrt, [16]). %% -> default pool |
| 76 | +{ok, Resp} = py:call(requests, get, [Url]). %% -> io pool (module registered) |
| 77 | +{ok, Rows} = py:call(db, query, [Sql]). %% -> io pool (function registered) |
| 78 | +{ok, Data} = py:call(db, connect, [Dsn]). %% -> default pool (only db.query registered) |
| 79 | +``` |
| 80 | + |
| 81 | +### Unregistering |
| 82 | + |
| 83 | +```erlang |
| 84 | +%% Remove module registration |
| 85 | +ok = py:unregister_pool(requests). |
| 86 | + |
| 87 | +%% Remove function registration |
| 88 | +ok = py:unregister_pool({urllib, urlopen}). |
| 89 | +``` |
| 90 | + |
| 91 | +## Registration Priority |
| 92 | + |
| 93 | +Function-specific registrations take priority over module-wide registrations: |
| 94 | + |
| 95 | +```erlang |
| 96 | +%% Register all json functions to io pool |
| 97 | +ok = py:register_pool(io, json). |
| 98 | + |
| 99 | +%% But keep json.dumps on default pool (it's fast) |
| 100 | +ok = py:register_pool(default, {json, dumps}). |
| 101 | + |
| 102 | +%% Results: |
| 103 | +io = py_context_router:lookup_pool(json, loads). %% Module registration |
| 104 | +default = py_context_router:lookup_pool(json, dumps). %% Function override |
| 105 | +``` |
| 106 | + |
| 107 | +## API Reference |
| 108 | + |
| 109 | +### High-Level API (py module) |
| 110 | + |
| 111 | +```erlang |
| 112 | +%% Register entire module to pool (all callables in the module) |
| 113 | +-spec register_pool(Pool, Module) -> ok when |
| 114 | + Pool :: default | io | atom(), |
| 115 | + Module :: atom(). |
| 116 | + |
| 117 | +%% Register specific callable (module.function) to pool |
| 118 | +-spec register_pool(Pool, {Module, Callable}) -> ok when |
| 119 | + Pool :: default | io | atom(), |
| 120 | + Module :: atom(), |
| 121 | + Callable :: atom(). |
| 122 | + |
| 123 | +%% Unregister module or specific callable |
| 124 | +-spec unregister_pool(Module | {Module, Callable}) -> ok. |
| 125 | + |
| 126 | +%% Call on specific pool |
| 127 | +-spec call(Pool, Module, Func, Args) -> {ok, Result} | {error, Reason}. |
| 128 | +-spec call(Pool, Module, Func, Args, Kwargs) -> {ok, Result} | {error, Reason}. |
| 129 | +``` |
| 130 | + |
| 131 | +### Low-Level API (py_context_router module) |
| 132 | + |
| 133 | +```erlang |
| 134 | +%% Pool management |
| 135 | +-spec start_pool(Pool, Size) -> {ok, [pid()]} | {error, term()}. |
| 136 | +-spec start_pool(Pool, Size, Mode) -> {ok, [pid()]} | {error, term()}. |
| 137 | +-spec stop_pool(Pool) -> ok. |
| 138 | +-spec pool_started(Pool) -> boolean(). |
| 139 | + |
| 140 | +%% Context access |
| 141 | +-spec get_context(Pool) -> pid(). |
| 142 | +-spec num_contexts(Pool) -> non_neg_integer(). |
| 143 | +-spec contexts(Pool) -> [pid()]. |
| 144 | + |
| 145 | +%% Registration |
| 146 | +-spec register_pool(Pool, Module) -> ok. |
| 147 | +-spec register_pool(Pool, Module, Func) -> ok. |
| 148 | +-spec unregister_pool(Module) -> ok. |
| 149 | +-spec unregister_pool(Module, Func) -> ok. |
| 150 | +-spec lookup_pool(Module, Func) -> Pool. |
| 151 | +-spec list_pool_registrations() -> [{{Module, Func | '_'}, Pool}]. |
| 152 | +``` |
| 153 | + |
| 154 | +## Configuration |
| 155 | + |
| 156 | +Configure pool sizes via application environment: |
| 157 | + |
| 158 | +```erlang |
| 159 | +%% sys.config |
| 160 | +[ |
| 161 | + {erlang_python, [ |
| 162 | + %% Default pool size (default: erlang:system_info(schedulers)) |
| 163 | + {default_pool_size, 8}, |
| 164 | + |
| 165 | + %% IO pool size (default: 10) |
| 166 | + {io_pool_size, 20}, |
| 167 | + |
| 168 | + %% IO pool mode: auto | subinterp | worker (default: auto) |
| 169 | + {io_pool_mode, worker} |
| 170 | + ]} |
| 171 | +]. |
| 172 | +``` |
| 173 | + |
| 174 | +### Runtime Configuration |
| 175 | + |
| 176 | +```erlang |
| 177 | +%% Start additional custom pool |
| 178 | +{ok, _} = py_context_router:start_pool(gpu, 2, worker). |
| 179 | + |
| 180 | +%% Register GPU operations |
| 181 | +ok = py:register_pool(gpu, torch). |
| 182 | +ok = py:register_pool(gpu, tensorflow). |
| 183 | +``` |
| 184 | + |
| 185 | +## Use Cases |
| 186 | + |
| 187 | +### Web Application with Database |
| 188 | + |
| 189 | +```erlang |
| 190 | +%% At application startup |
| 191 | +init_pools() -> |
| 192 | + %% Register I/O-heavy modules |
| 193 | + py:register_pool(io, requests), |
| 194 | + py:register_pool(io, httpx), |
| 195 | + py:register_pool(io, psycopg2), |
| 196 | + py:register_pool(io, redis), |
| 197 | + ok. |
| 198 | + |
| 199 | +%% In request handler - no pool awareness needed |
| 200 | +handle_request(UserId) -> |
| 201 | + %% Fast: uses default pool |
| 202 | + {ok, Hash} = py:call(hashlib, sha256, [UserId]), |
| 203 | + |
| 204 | + %% Slow: automatically uses io pool |
| 205 | + {ok, User} = py:call(psycopg2, fetchone, [<<"SELECT * FROM users WHERE id = ?">>, [UserId]]), |
| 206 | + |
| 207 | + %% Fast: uses default pool |
| 208 | + {ok, Json} = py:call(json, dumps, [User]), |
| 209 | + {ok, Json}. |
| 210 | +``` |
| 211 | + |
| 212 | +### ML Pipeline with I/O |
| 213 | + |
| 214 | +```erlang |
| 215 | +%% Register I/O operations |
| 216 | +py:register_pool(io, boto3), %% S3 access |
| 217 | +py:register_pool(io, requests), %% API calls |
| 218 | + |
| 219 | +%% ML operations stay on default pool (CPU-intensive) |
| 220 | +%% I/O operations go to io pool |
| 221 | + |
| 222 | +process_batch(Items) -> |
| 223 | + %% Parallel fetch from S3 (io pool) |
| 224 | + Futures = [py:cast(boto3, download_file, [Key]) || Key <- Items], |
| 225 | + Files = [py:await(F) || F <- Futures], |
| 226 | + |
| 227 | + %% Process with ML model (default pool - doesn't block I/O) |
| 228 | + [{ok, _} = py:call(model, predict, [File]) || File <- Files]. |
| 229 | +``` |
| 230 | + |
| 231 | +## Performance Considerations |
| 232 | + |
| 233 | +### Pool Sizing |
| 234 | + |
| 235 | +| Workload | default Pool | io Pool | |
| 236 | +|----------|--------------|---------| |
| 237 | +| CPU-heavy | Schedulers | Small (5-10) | |
| 238 | +| I/O-heavy | Small (2-4) | Large (20-50) | |
| 239 | +| Mixed | Schedulers | 10-20 | |
| 240 | + |
| 241 | +### When to Use Registration |
| 242 | + |
| 243 | +**Use registration when:** |
| 244 | +- You have clear I/O-bound modules (HTTP clients, database drivers) |
| 245 | +- You want transparent routing without changing call sites |
| 246 | +- Multiple call sites use the same module |
| 247 | + |
| 248 | +**Use explicit pool selection when:** |
| 249 | +- A function's pool depends on arguments |
| 250 | +- You need fine-grained control per-call |
| 251 | +- Testing or debugging specific pools |
| 252 | + |
| 253 | +## Monitoring |
| 254 | + |
| 255 | +```erlang |
| 256 | +%% Check pool status |
| 257 | +true = py_context_router:pool_started(default), |
| 258 | +true = py_context_router:pool_started(io). |
| 259 | + |
| 260 | +%% Check pool sizes |
| 261 | +DefaultSize = py_context_router:num_contexts(default), |
| 262 | +IoSize = py_context_router:num_contexts(io). |
| 263 | + |
| 264 | +%% List all registrations |
| 265 | +Registrations = py_context_router:list_pool_registrations(). |
| 266 | +%% => [{{requests, '_'}, io}, {{json, dumps}, default}, ...] |
| 267 | + |
| 268 | +%% Check which pool a call would use |
| 269 | +io = py_context_router:lookup_pool(requests, get). |
| 270 | +default = py_context_router:lookup_pool(math, sqrt). |
| 271 | +``` |
| 272 | + |
| 273 | +## Backward Compatibility |
| 274 | + |
| 275 | +Existing code using `py:call/3,4,5` without pool registration continues to work unchanged, using the default pool: |
| 276 | + |
| 277 | +```erlang |
| 278 | +%% These all use the default pool (backward compatible) |
| 279 | +{ok, 4.0} = py:call(math, sqrt, [16]). |
| 280 | +{ok, Data} = py:call(json, dumps, [#{a => 1}]). |
| 281 | +{ok, 6} = py:eval(<<"2 + 4">>). |
| 282 | +``` |
| 283 | + |
| 284 | +## See Also |
| 285 | + |
| 286 | +- [Scalability](scalability.md) - Execution modes and parallel execution |
| 287 | +- [Getting Started](getting-started.md) - Basic usage |
| 288 | +- [Asyncio](asyncio.md) - Async I/O with event loops |
0 commit comments