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