1- # Dual Pool Support
1+ # Pool Support
22
3- This guide covers erlang_python's dual pool architecture for separating CPU-bound and I/O-bound Python operations.
3+ This guide covers erlang_python's pool architecture for separating CPU-bound and I/O-bound Python operations.
44
55## Overview
66
7- erlang_python provides two separate pools of Python contexts :
7+ erlang_python provides a ` default ` pool that starts automatically, and allows you to create additional pools on demand :
88
99| Pool | Purpose | Default Size | Use Case |
1010| ------| ---------| --------------| ----------|
1111| ` 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 |
12+ | custom pools | User-defined pools | User-defined | HTTP requests, database queries, GPU work |
1313
14- This separation prevents slow I/O operations from blocking quick CPU operations.
14+ Create pools on demand to separate slow I/O operations from blocking quick CPU operations.
1515
1616## Architecture
1717
@@ -28,19 +28,31 @@ This separation prevents slow I/O operations from blocking quick CPU operations.
2828│ ┌──────────────┴──────────────┐ │
2929│ ▼ ▼ │
3030│ ┌────────────────┐ ┌────────────────┐ │
31- │ │ default pool │ │ io pool │ │
32- │ │ (N contexts) │ │ (10 contexts) │ │
31+ │ │ default pool │ │ custom pools │ │
32+ │ │ (N contexts) │ │ (on demand) │ │
3333│ └────────────────┘ └────────────────┘ │
3434│ │ │ │
3535│ ┌────────┴────────┐ ┌────────┴────────┐ │
3636│ ▼ ▼ ▼ ▼ ▼ ▼ │
37- │ Ctx1 Ctx2 CtxN Ctx1 Ctx2 Ctx10 │
37+ │ Ctx1 Ctx2 CtxN Ctx1 Ctx2 CtxN │
3838│ (math) (json) (...) (http) (db) (...) │
3939└──────────────────────────────────────────────────────────────────┘
4040```
4141
4242## Basic Usage
4343
44+ ### Creating Custom Pools
45+
46+ Create pools on demand for specific workloads:
47+
48+ ``` erlang
49+ % % Create an io pool for I/O-bound operations
50+ {ok , _Contexts } = py_context_router :start_pool (io , 10 , worker ).
51+
52+ % % Create a gpu pool for ML workloads
53+ {ok , _ } = py_context_router :start_pool (gpu , 2 , worker ).
54+ ```
55+
4456### Explicit Pool Selection
4557
4658Specify the pool directly in the call:
@@ -49,7 +61,7 @@ Specify the pool directly in the call:
4961% % Use default pool (quick operations)
5062{ok , 4.0 } = py :call (default , math , sqrt , [16 ]).
5163
52- % % Use io pool (slow operations )
64+ % % Use io pool (after creating it )
5365{ok , Response } = py :call (io , requests , get , [Url ]).
5466
5567% % With keyword arguments
@@ -153,31 +165,30 @@ default = py_context_router:lookup_pool(json, dumps). %% Function override
153165
154166## Configuration
155167
156- Configure pool sizes via application environment:
168+ Configure default pool size via application environment:
157169
158170``` erlang
159171% % sys.config
160172[
161173 {erlang_python , [
162174 % % 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 }
175+ {default_pool_size , 8 }
170176 ]}
171177].
172178```
173179
174180### Runtime Configuration
175181
176182``` erlang
177- % % Start additional custom pool
183+ % % Start io pool for I/O-bound operations
184+ {ok , _ } = py_context_router :start_pool (io , 10 , worker ).
185+
186+ % % Start GPU pool for ML operations
178187{ok , _ } = py_context_router :start_pool (gpu , 2 , worker ).
179188
180- % % Register GPU operations
189+ % % Register operations to route to specific pools
190+ ok = py :register_pool (io , requests ).
191+ ok = py :register_pool (io , psycopg2 ).
181192ok = py :register_pool (gpu , torch ).
182193ok = py :register_pool (gpu , tensorflow ).
183194```
@@ -189,6 +200,9 @@ ok = py:register_pool(gpu, tensorflow).
189200``` erlang
190201% % At application startup
191202init_pools () ->
203+ % % Create io pool for I/O-bound operations
204+ {ok , _ } = py_context_router :start_pool (io , 10 , worker ),
205+
192206 % % Register I/O-heavy modules
193207 py :register_pool (io , requests ),
194208 py :register_pool (io , httpx ),
@@ -212,7 +226,8 @@ handle_request(UserId) ->
212226### ML Pipeline with I/O
213227
214228``` erlang
215- % % Register I/O operations
229+ % % Create and configure io pool
230+ {ok , _ } = py_context_router :start_pool (io , 10 , worker ),
216231py :register_pool (io , boto3 ), % % S3 access
217232py :register_pool (io , requests ), % % API calls
218233
@@ -254,11 +269,15 @@ process_batch(Items) ->
254269
255270``` erlang
256271% % Check pool status
257- true = py_context_router :pool_started (default ),
272+ true = py_context_router :pool_started (default ).
273+ false = py_context_router :pool_started (io ). % % Not started yet
274+
275+ % % Start io pool
276+ {ok , _ } = py_context_router :start_pool (io , 10 , worker ).
258277true = py_context_router :pool_started (io ).
259278
260279% % Check pool sizes
261- DefaultSize = py_context_router :num_contexts (default ),
280+ DefaultSize = py_context_router :num_contexts (default ).
262281IoSize = py_context_router :num_contexts (io ).
263282
264283% % List all registrations
0 commit comments