multiloop 0.1.0__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
multiloop/__init__.py ADDED
@@ -0,0 +1,121 @@
1
+ """multiloop: Multi-event-loop engine & concurrency toolkit for Python 3.14t.
2
+
3
+ A high-performance, Go-like concurrency library for free-threaded Python,
4
+ built around a Rust core. Key capabilities:
5
+
6
+ - EventLoopThreadPool with round-robin scheduling and work stealing
7
+ - Channel and select_channel for Go-style communication
8
+ - CancelScope and TaskGroup for structured concurrency
9
+ - AsyncContext, AsyncWaitGroup, AsyncOnce, AsyncRWMutex primitives
10
+ - ASGI worker (MultiloopASGIWorker) for FastAPI-style servers
11
+
12
+ Docs: https://github.com/hankaihong1/multiloop (docs/API.md, docs/sphinx_html/).
13
+ """
14
+
15
+ from typing import Any
16
+
17
+ from multiloop._cancel import (
18
+ CancelScope,
19
+ checkpoint,
20
+ current_effective_deadline,
21
+ fail_after,
22
+ fail_at,
23
+ move_on_after,
24
+ move_on_at,
25
+ )
26
+ from multiloop._logging import get_logger, set_log_level
27
+ from multiloop._sync import (
28
+ Barrier,
29
+ BarrierWaitResult,
30
+ CapacityLimiter,
31
+ Condition,
32
+ Event,
33
+ Lock,
34
+ Semaphore,
35
+ )
36
+ from multiloop._taskgroup import TaskGroup, TaskHandle, TaskStatus
37
+ from multiloop.asgi import MultiloopASGIWorker
38
+ from multiloop.context import AsyncContext
39
+ from multiloop.exceptions import (
40
+ ChannelClosedError,
41
+ MultiloopError,
42
+ ThreadPoolClosedError,
43
+ TimeoutError,
44
+ WouldBlock,
45
+ )
46
+ from multiloop.pool import (
47
+ EventLoopThreadPool,
48
+ PoolOptions,
49
+ create_pool,
50
+ )
51
+ from multiloop.primitives import (
52
+ AsyncOnce,
53
+ AsyncWaitGroup,
54
+ Channel,
55
+ select_channel,
56
+ )
57
+ from multiloop.rwlock import AsyncRWMutex
58
+ from multiloop.server import ConnectionPinningServer
59
+ from multiloop.wsgi import MultiloopWSGIWorker
60
+
61
+ __version__ = "0.1.0"
62
+
63
+
64
+ async def run_in_pool(coro: Any, *args: Any, num_threads: int = 0, **kwargs: Any) -> Any:
65
+ """Run a coroutine in a freshly-created pool (one-shot convenience).
66
+
67
+ :param coro: Coroutine function or coroutine object to run.
68
+ :param num_threads: Worker thread count (0 for auto-detection).
69
+ :param args: Positional arguments for ``coro``.
70
+ :param kwargs: Keyword arguments for ``coro``.
71
+ :returns: Result returned by ``coro``.
72
+ """
73
+ pool = EventLoopThreadPool(num_threads=num_threads)
74
+ await pool.start()
75
+ try:
76
+ fut = pool.submit(coro, *args, **kwargs)
77
+ return await fut
78
+ finally:
79
+ await pool.close()
80
+
81
+
82
+ __all__ = [
83
+ "AsyncContext",
84
+ "AsyncOnce",
85
+ "AsyncRWMutex",
86
+ "AsyncWaitGroup",
87
+ "Barrier",
88
+ "BarrierWaitResult",
89
+ "CancelScope",
90
+ "CapacityLimiter",
91
+ "Channel",
92
+ "ChannelClosedError",
93
+ "Condition",
94
+ "ConnectionPinningServer",
95
+ "Event",
96
+ "EventLoopThreadPool",
97
+ "Lock",
98
+ "MultiloopASGIWorker",
99
+ "MultiloopError",
100
+ "MultiloopWSGIWorker",
101
+ "PoolOptions",
102
+ "Semaphore",
103
+ "TaskGroup",
104
+ "TaskHandle",
105
+ "TaskStatus",
106
+ "ThreadPoolClosedError",
107
+ "TimeoutError",
108
+ "WouldBlock",
109
+ "__version__",
110
+ "checkpoint",
111
+ "create_pool",
112
+ "current_effective_deadline",
113
+ "fail_after",
114
+ "fail_at",
115
+ "get_logger",
116
+ "move_on_after",
117
+ "move_on_at",
118
+ "run_in_pool",
119
+ "select_channel",
120
+ "set_log_level",
121
+ ]
multiloop/__init__.pyi ADDED
@@ -0,0 +1,451 @@
1
+ """Type stubs for multiloop package."""
2
+
3
+ import asyncio
4
+ import enum
5
+ import logging
6
+ import types
7
+ from collections.abc import AsyncGenerator, AsyncIterator, Callable
8
+ from typing import Any, Self
9
+
10
+ from multiloop.exceptions import (
11
+ ChannelClosedError,
12
+ MultiloopError,
13
+ ThreadPoolClosedError,
14
+ TimeoutError,
15
+ WouldBlock,
16
+ )
17
+
18
+ __all__ = [
19
+ "AsyncContext",
20
+ "AsyncOnce",
21
+ "AsyncRWMutex",
22
+ "AsyncWaitGroup",
23
+ "Barrier",
24
+ "BarrierWaitResult",
25
+ "CancelScope",
26
+ "CapacityLimiter",
27
+ "Channel",
28
+ "ChannelClosedError",
29
+ "Condition",
30
+ "ConnectionPinningServer",
31
+ "Event",
32
+ "EventLoopThreadPool",
33
+ "Lock",
34
+ "MultiloopASGIWorker",
35
+ "MultiloopError",
36
+ "MultiloopWSGIWorker",
37
+ "PoolOptions",
38
+ "Semaphore",
39
+ "TaskGroup",
40
+ "TaskHandle",
41
+ "TaskStatus",
42
+ "ThreadPoolClosedError",
43
+ "TimeoutError",
44
+ "WouldBlock",
45
+ "__version__",
46
+ "checkpoint",
47
+ "create_pool",
48
+ "current_effective_deadline",
49
+ "fail_after",
50
+ "fail_at",
51
+ "get_logger",
52
+ "move_on_after",
53
+ "move_on_at",
54
+ "run_in_pool",
55
+ "select_channel",
56
+ "set_log_level",
57
+ ]
58
+
59
+ __version__: str
60
+
61
+ # -- PoolOptions ----------------------------------------------------------
62
+
63
+ class PoolOptions:
64
+ num_threads: int
65
+ loop_factory: Callable[[], asyncio.AbstractEventLoop] | None
66
+
67
+ def __init__(
68
+ self,
69
+ num_threads: int = 0,
70
+ loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None,
71
+ ) -> None: ...
72
+
73
+ # -- CancelScope ----------------------------------------------------------
74
+
75
+ class CancelScope:
76
+ def __init__(self, deadline: float = float("inf"), shield: bool = False) -> None: ...
77
+ @property
78
+ def cancel_called(self) -> bool: ...
79
+ @property
80
+ def deadline(self) -> float: ...
81
+ @deadline.setter
82
+ def deadline(self, value: float) -> None: ...
83
+ @property
84
+ def shield(self) -> bool: ...
85
+ @shield.setter
86
+ def shield(self, value: bool) -> None: ...
87
+ @property
88
+ def cancelled_caught(self) -> bool: ...
89
+ def cancel(self) -> None: ...
90
+ async def __aenter__(self) -> Self: ...
91
+ async def __aexit__(
92
+ self,
93
+ exc_type: type[BaseException] | None,
94
+ exc_val: BaseException | None,
95
+ exc_tb: object,
96
+ ) -> bool | None: ...
97
+
98
+ # -- EventLoopThreadPool --------------------------------------------------
99
+
100
+ class EventLoopThreadPool:
101
+ num_threads: int
102
+
103
+ def __init__(
104
+ self,
105
+ options: PoolOptions | None = None,
106
+ num_threads: int | None = None,
107
+ loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None,
108
+ ) -> None: ...
109
+ @property
110
+ def is_running(self) -> bool: ...
111
+ def get_metrics(self) -> dict[str, Any]: ...
112
+ async def start(self) -> None: ...
113
+ async def close(self) -> None: ...
114
+ async def abort(self) -> None: ...
115
+ async def wait_closed(self) -> None: ...
116
+ async def __aenter__(self) -> Self: ...
117
+ async def __aexit__(
118
+ self,
119
+ exc_type: type[BaseException] | None,
120
+ exc_val: BaseException | None,
121
+ exc_tb: types.TracebackType | None,
122
+ ) -> None: ...
123
+ def submit(
124
+ self,
125
+ target: Callable[..., Any] | Any,
126
+ *args: Any,
127
+ pin_to: asyncio.AbstractEventLoop | int | None = None,
128
+ cancel_scope: CancelScope | None = None,
129
+ **kwargs: Any,
130
+ ) -> asyncio.Future[Any]: ...
131
+
132
+ # -- Channel --------------------------------------------------------------
133
+
134
+ class Channel:
135
+ def __init__(self, maxsize: int = 0) -> None: ...
136
+ def close(self) -> None: ...
137
+ @property
138
+ def is_closed(self) -> bool: ...
139
+ @property
140
+ def maxsize(self) -> int: ...
141
+ def empty(self) -> bool: ...
142
+ def full(self) -> bool: ...
143
+ async def send(self, item: Any) -> None: ...
144
+ def send_sync(self, item: Any, timeout: float | None = None) -> None: ...
145
+ async def recv(self, timeout: float | None = None) -> Any: ...
146
+ def try_send(self, item: Any) -> bool: ...
147
+ def try_recv(self) -> Any: ...
148
+ def qsize(self) -> int: ...
149
+ def __aiter__(self) -> AsyncIterator[Any]: ...
150
+ async def __anext__(self) -> Any: ...
151
+
152
+ # -- select_channel -------------------------------------------------------
153
+
154
+ async def select_channel(
155
+ *channels: Any, timeout: float | None = None, default: Any = ...
156
+ ) -> Any: ...
157
+
158
+ # -- AsyncContext ---------------------------------------------------------
159
+
160
+ class AsyncContext:
161
+ @property
162
+ def parent(self) -> AsyncContext | None: ...
163
+ def __init__(self, parent: AsyncContext | None = None) -> None: ...
164
+ @property
165
+ def is_cancelled(self) -> bool: ...
166
+ def submit(
167
+ self,
168
+ pool: EventLoopThreadPool,
169
+ target: Callable[..., Any],
170
+ *args: Any,
171
+ **kwargs: Any,
172
+ ) -> asyncio.Future[Any]: ...
173
+ def cancel(self) -> None: ...
174
+ async def __aenter__(self) -> Self: ...
175
+ async def __aexit__(
176
+ self,
177
+ exc_type: type[BaseException] | None,
178
+ exc_val: BaseException | None,
179
+ exc_tb: object,
180
+ ) -> None: ...
181
+
182
+ # -- AsyncWaitGroup -------------------------------------------------------
183
+
184
+ class AsyncWaitGroup:
185
+ def __init__(self) -> None: ...
186
+ def add(self, delta: int = 1) -> None: ...
187
+ def done(self) -> None: ...
188
+ def holding(self) -> AsyncGenerator[None]: ...
189
+ def wrap(self, fn: Callable[..., Any]) -> Callable[..., Any]: ...
190
+ def track(self, coro: Any) -> Any: ...
191
+ async def wait(self) -> None: ...
192
+
193
+ # -- AsyncOnce ------------------------------------------------------------
194
+
195
+ class AsyncOnce:
196
+ def __init__(self) -> None: ...
197
+ async def do(self, fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: ...
198
+
199
+ # -- AsyncRWMutex ---------------------------------------------------------
200
+
201
+ class AsyncRWMutex:
202
+ def __init__(self) -> None: ...
203
+ def reader(self) -> AsyncGenerator[None]: ...
204
+ def writer(self) -> AsyncGenerator[None]: ...
205
+
206
+ # -- ConnectionPinningServer ----------------------------------------------
207
+
208
+ class ConnectionPinningServer:
209
+ pool: EventLoopThreadPool
210
+ host: str
211
+ port: int
212
+ handler: Callable[[asyncio.StreamReader, asyncio.StreamWriter], Any] | None
213
+
214
+ def __init__(
215
+ self,
216
+ pool: EventLoopThreadPool,
217
+ host: str = "127.0.0.1",
218
+ port: int = 0,
219
+ handler: Callable[[asyncio.StreamReader, asyncio.StreamWriter], Any] | None = None,
220
+ ) -> None: ...
221
+ @property
222
+ def is_running(self) -> bool: ...
223
+ async def start(
224
+ self,
225
+ handler: Callable[[asyncio.StreamReader, asyncio.StreamWriter], Any] | None = None,
226
+ ) -> None: ...
227
+ async def close(self) -> None: ...
228
+ async def __aenter__(self) -> Self: ...
229
+ async def __aexit__(
230
+ self,
231
+ exc_type: type[BaseException] | None,
232
+ exc_val: BaseException | None,
233
+ exc_tb: types.TracebackType | None,
234
+ ) -> None: ...
235
+
236
+ # -- MultiloopASGIWorker --------------------------------------------------
237
+
238
+ class MultiloopASGIWorker:
239
+ app: Callable[..., Any]
240
+ pool: EventLoopThreadPool
241
+ host: str
242
+ port: int
243
+ lifespan: str
244
+
245
+ def __init__(
246
+ self,
247
+ app: Callable[..., Any],
248
+ pool: EventLoopThreadPool,
249
+ host: str = "127.0.0.1",
250
+ port: int = 0,
251
+ lifespan: str = "auto",
252
+ ) -> None: ...
253
+ @property
254
+ def is_running(self) -> bool: ...
255
+ async def start(self) -> None: ...
256
+ async def close(self) -> None: ...
257
+ async def __aenter__(self) -> Self: ...
258
+ async def __aexit__(
259
+ self,
260
+ exc_type: type[BaseException] | None,
261
+ exc_val: BaseException | None,
262
+ exc_tb: types.TracebackType | None,
263
+ ) -> None: ...
264
+
265
+ # -- MultiloopWSGIWorker --------------------------------------------------
266
+
267
+ class MultiloopWSGIWorker:
268
+ app: Callable[..., Any]
269
+ pool: EventLoopThreadPool
270
+ host: str
271
+ port: int
272
+
273
+ def __init__(
274
+ self,
275
+ app: Callable[..., Any],
276
+ pool: EventLoopThreadPool,
277
+ host: str = "127.0.0.1",
278
+ port: int = 0,
279
+ ) -> None: ...
280
+ @property
281
+ def is_running(self) -> bool: ...
282
+ async def start(self) -> None: ...
283
+ async def close(self) -> None: ...
284
+ async def __aenter__(self) -> Self: ...
285
+ async def __aexit__(
286
+ self,
287
+ exc_type: type[BaseException] | None,
288
+ exc_val: BaseException | None,
289
+ exc_tb: types.TracebackType | None,
290
+ ) -> None: ...
291
+
292
+ # -- Lock -----------------------------------------------------------------
293
+
294
+ class Lock:
295
+ def __init__(self) -> None: ...
296
+ @property
297
+ def locked(self) -> bool: ...
298
+ @property
299
+ def owner(self) -> asyncio.Task[Any] | None: ...
300
+ async def acquire(self) -> None: ...
301
+ def release(self) -> None: ...
302
+ async def __aenter__(self) -> Self: ...
303
+ async def __aexit__(
304
+ self,
305
+ exc_type: type[BaseException] | None,
306
+ exc_val: BaseException | None,
307
+ exc_tb: object,
308
+ ) -> bool | None: ...
309
+
310
+ # -- Semaphore ------------------------------------------------------------
311
+
312
+ class Semaphore:
313
+ def __init__(self, max_value: int) -> None: ...
314
+ @property
315
+ def value(self) -> int: ...
316
+ @property
317
+ def max_value(self) -> int: ...
318
+ async def acquire(self) -> None: ...
319
+ def release(self) -> None: ...
320
+ async def __aenter__(self) -> Self: ...
321
+ async def __aexit__(
322
+ self,
323
+ exc_type: type[BaseException] | None,
324
+ exc_val: BaseException | None,
325
+ exc_tb: object | None,
326
+ ) -> bool | None: ...
327
+
328
+ # -- CapacityLimiter ------------------------------------------------------
329
+
330
+ class CapacityLimiter:
331
+ def __init__(self, total_tokens: float) -> None: ...
332
+ @property
333
+ def total_capacity(self) -> int: ...
334
+ @property
335
+ def available_capacity(self) -> int: ...
336
+ @property
337
+ def total_tokens(self) -> float: ...
338
+ @total_tokens.setter
339
+ def total_tokens(self, value: float) -> None: ...
340
+ @property
341
+ def available_tokens(self) -> float: ...
342
+ @property
343
+ def borrowed_tokens(self) -> float: ...
344
+ def snapshot(self) -> tuple[float, float, float]: ...
345
+ async def acquire(self) -> None: ...
346
+ def release(self) -> None: ...
347
+ async def __aenter__(self) -> Self: ...
348
+ async def __aexit__(
349
+ self,
350
+ exc_type: type[BaseException] | None,
351
+ exc_val: BaseException | None,
352
+ exc_tb: object | None,
353
+ ) -> bool | None: ...
354
+
355
+ # -- Event ----------------------------------------------------------------
356
+
357
+ class Event:
358
+ def __init__(self) -> None: ...
359
+ def is_set(self) -> bool: ...
360
+ def set(self) -> None: ...
361
+ async def wait(self) -> None: ...
362
+
363
+ # -- Condition ------------------------------------------------------------
364
+
365
+ class Condition:
366
+ def __init__(self, lock: Lock | None = None) -> None: ...
367
+ async def acquire(self) -> None: ...
368
+ def release(self) -> None: ...
369
+ async def wait(self) -> None: ...
370
+ async def wait_for(self, predicate: Callable[[], Any]) -> Any: ...
371
+ def notify(self, n: int = 1) -> None: ...
372
+ def notify_all(self) -> None: ...
373
+ async def __aenter__(self) -> Self: ...
374
+ async def __aexit__(
375
+ self,
376
+ exc_type: type[BaseException] | None,
377
+ exc_val: BaseException | None,
378
+ exc_tb: object,
379
+ ) -> bool | None: ...
380
+
381
+ # -- Barrier --------------------------------------------------------------
382
+
383
+ class BarrierWaitResult:
384
+ parties: int
385
+ index: int
386
+
387
+ def __init__(self, parties: int, index: int = ...) -> None: ...
388
+ @property
389
+ def is_leader(self) -> bool: ...
390
+
391
+ class Barrier:
392
+ def __init__(self, parties: int) -> None: ...
393
+ @property
394
+ def parties(self) -> int: ...
395
+ @property
396
+ def broken(self) -> bool: ...
397
+ @property
398
+ def n_waiting(self) -> int: ...
399
+ async def wait(self) -> BarrierWaitResult: ...
400
+ def abort(self) -> None: ...
401
+ def reset(self) -> None: ...
402
+
403
+ # -- TaskGroup / TaskHandle / TaskStatus ----------------------------------
404
+
405
+ class _TaskStatus(enum.Enum):
406
+ PENDING = ...
407
+ STARTED = ...
408
+ FINISHED = ...
409
+
410
+ class TaskStatus:
411
+ def __init__(self) -> None: ...
412
+ def started(self) -> None: ...
413
+
414
+ class TaskHandle:
415
+ @property
416
+ def status(self) -> _TaskStatus: ...
417
+ @property
418
+ def result(self) -> Any: ...
419
+ @property
420
+ def exception(self) -> BaseException | None: ...
421
+ def __await__(self) -> Any: ...
422
+
423
+ class TaskGroup:
424
+ def __init__(self, name: str | None = None) -> None: ...
425
+ def start_soon(self, coro_fn: Callable[..., Any], *args: Any) -> TaskHandle: ...
426
+ async def start(self, coro_fn: Callable[..., Any], *args: Any) -> TaskHandle: ...
427
+ def cancel_all(self) -> None: ...
428
+ async def __aenter__(self) -> Self: ...
429
+ async def __aexit__(
430
+ self,
431
+ exc_type: type[BaseException] | None,
432
+ exc_val: BaseException | None,
433
+ exc_tb: object,
434
+ ) -> bool | None: ...
435
+
436
+ # -- Top-level convenience functions ---------------------------------------
437
+
438
+ async def create_pool(
439
+ num_threads: int = 0,
440
+ options: PoolOptions | None = None,
441
+ **kwargs: Any,
442
+ ) -> EventLoopThreadPool: ...
443
+ async def run_in_pool(coro: Any, *args: Any, num_threads: int = 0, **kwargs: Any) -> Any: ...
444
+ async def checkpoint() -> None: ...
445
+ def current_effective_deadline() -> float: ...
446
+ def fail_after(seconds: float) -> CancelScope: ...
447
+ def move_on_after(seconds: float) -> CancelScope: ...
448
+ def fail_at(deadline: float) -> CancelScope: ...
449
+ def move_on_at(deadline: float) -> CancelScope: ...
450
+ def get_logger(name: str | None = None) -> logging.LoggerAdapter[Any]: ...
451
+ def set_log_level(level: int) -> None: ...