flpit 0.4.2.dev4__py3-none-any.whl → 0.4.2.dev5__py3-none-any.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.
- flpit/task.py +110 -15
- flpit/threading.py +15 -1
- {flpit-0.4.2.dev4.dist-info → flpit-0.4.2.dev5.dist-info}/METADATA +1 -1
- {flpit-0.4.2.dev4.dist-info → flpit-0.4.2.dev5.dist-info}/RECORD +6 -6
- {flpit-0.4.2.dev4.dist-info → flpit-0.4.2.dev5.dist-info}/WHEEL +0 -0
- {flpit-0.4.2.dev4.dist-info → flpit-0.4.2.dev5.dist-info}/licenses/LICENSE +0 -0
flpit/task.py
CHANGED
|
@@ -421,6 +421,9 @@ class _TimerQueue:
|
|
|
421
421
|
break
|
|
422
422
|
if due:
|
|
423
423
|
break
|
|
424
|
+
# Not while waiting: the next entry's callback references
|
|
425
|
+
# its task (a cancelled delay, say) until its deadline.
|
|
426
|
+
handle = callback = None
|
|
424
427
|
if heap:
|
|
425
428
|
condition.wait(min(heap[0][0] - now, park))
|
|
426
429
|
else:
|
|
@@ -431,6 +434,7 @@ class _TimerQueue:
|
|
|
431
434
|
except BaseException:
|
|
432
435
|
_LOG.exception("Unhandled exception in Task timer callback")
|
|
433
436
|
due.clear()
|
|
437
|
+
callback = None # the last one ran: do not keep its task while waiting
|
|
434
438
|
|
|
435
439
|
|
|
436
440
|
def _fresh_context() -> dict[str, Any]:
|
|
@@ -1107,7 +1111,10 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1107
1111
|
task._run_coroutine_inline(state, work, args, kwargs)
|
|
1108
1112
|
else:
|
|
1109
1113
|
runtime._submit_inline(task, work, args, kwargs, state)
|
|
1110
|
-
|
|
1114
|
+
try:
|
|
1115
|
+
return task
|
|
1116
|
+
finally:
|
|
1117
|
+
del task, work, args, kwargs # see _release_frame
|
|
1111
1118
|
|
|
1112
1119
|
@classmethod
|
|
1113
1120
|
def _create(
|
|
@@ -1360,7 +1367,14 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1360
1367
|
"""
|
|
1361
1368
|
if self._state is _PENDING:
|
|
1362
1369
|
self._wait()
|
|
1363
|
-
|
|
1370
|
+
error = self._error_to_raise()
|
|
1371
|
+
if error is None:
|
|
1372
|
+
return cast(T, self._result)
|
|
1373
|
+
del self # see _release_frame
|
|
1374
|
+
try:
|
|
1375
|
+
raise error
|
|
1376
|
+
finally:
|
|
1377
|
+
del error
|
|
1364
1378
|
|
|
1365
1379
|
# ------------------------------------------------------------------
|
|
1366
1380
|
# Blocking waits (.NET Task.Wait / Task.WaitAll / Task.WaitAny)
|
|
@@ -1552,7 +1566,12 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1552
1566
|
if self._state is _PENDING:
|
|
1553
1567
|
self._wait()
|
|
1554
1568
|
if self._state is _CANCELLED:
|
|
1555
|
-
|
|
1569
|
+
error = self._cancellation_exception()
|
|
1570
|
+
del self # see _release_frame
|
|
1571
|
+
try:
|
|
1572
|
+
raise error
|
|
1573
|
+
finally:
|
|
1574
|
+
del error
|
|
1556
1575
|
return self._exception
|
|
1557
1576
|
|
|
1558
1577
|
def _wait(self) -> None:
|
|
@@ -1570,6 +1589,9 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1570
1589
|
and state.inline < _MAX_INLINE_DEPTH
|
|
1571
1590
|
):
|
|
1572
1591
|
self._run_inline(state)
|
|
1592
|
+
if self._state is not _PENDING:
|
|
1593
|
+
del self # see _release_frame
|
|
1594
|
+
return
|
|
1573
1595
|
with self._lock:
|
|
1574
1596
|
if self._state is not _PENDING:
|
|
1575
1597
|
return
|
|
@@ -1598,6 +1620,7 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1598
1620
|
self._start()
|
|
1599
1621
|
finally:
|
|
1600
1622
|
_exit_inline(state, tail, hooks)
|
|
1623
|
+
del self # see _release_frame
|
|
1601
1624
|
|
|
1602
1625
|
def _run_coroutine_inline(
|
|
1603
1626
|
self, state: _ThreadState, target: Any, args: tuple[Any, ...], kwargs: dict[str, Any]
|
|
@@ -1645,6 +1668,7 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1645
1668
|
depth = state.inline
|
|
1646
1669
|
state.inline = depth + 1
|
|
1647
1670
|
drove = False
|
|
1671
|
+
coroutine: Any = None
|
|
1648
1672
|
try:
|
|
1649
1673
|
context.run(_current_task._value.set, self)
|
|
1650
1674
|
try:
|
|
@@ -1696,6 +1720,7 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1696
1720
|
_exit_inline_slot(state, tail)
|
|
1697
1721
|
if hooks is not None:
|
|
1698
1722
|
sys.set_asyncgen_hooks(*hooks)
|
|
1723
|
+
del self, context, target, args, kwargs, coroutine # see _release_frame
|
|
1699
1724
|
|
|
1700
1725
|
def _add_continuation(self, continuation: Callable[[Task[Any]], Any]) -> bool:
|
|
1701
1726
|
"""Call ``continuation(self)`` on completion; False if already complete.
|
|
@@ -1728,9 +1753,20 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1728
1753
|
|
|
1729
1754
|
def _await_pure_flpit_task(self) -> Generator[Any, T, T]:
|
|
1730
1755
|
if self._state is not _PENDING:
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1756
|
+
error = self._error_to_raise()
|
|
1757
|
+
if error is None:
|
|
1758
|
+
return cast(T, self._result)
|
|
1759
|
+
del self # see _release_frame
|
|
1760
|
+
try:
|
|
1761
|
+
raise error
|
|
1762
|
+
finally:
|
|
1763
|
+
del error
|
|
1764
|
+
operation = _WaitTask(self._runtime, self)
|
|
1765
|
+
del self # see _release_frame
|
|
1766
|
+
try:
|
|
1767
|
+
return (yield operation)
|
|
1768
|
+
finally:
|
|
1769
|
+
del operation
|
|
1734
1770
|
|
|
1735
1771
|
__await__ = _await_pure_flpit_task
|
|
1736
1772
|
|
|
@@ -1772,8 +1808,33 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1772
1808
|
yield from cast(Generator[Any, T, T], future.__await__())
|
|
1773
1809
|
)
|
|
1774
1810
|
|
|
1775
|
-
|
|
1776
|
-
|
|
1811
|
+
operation = _WaitTask(self._runtime, self)
|
|
1812
|
+
del self # see _release_frame
|
|
1813
|
+
try:
|
|
1814
|
+
return (yield operation)
|
|
1815
|
+
finally:
|
|
1816
|
+
del operation
|
|
1817
|
+
|
|
1818
|
+
def _error_to_raise(self) -> BaseException | None:
|
|
1819
|
+
"""The exception that awaiting this completed task raises; None if it succeeded.
|
|
1820
|
+
|
|
1821
|
+
_release_frame: a failed task's exception keeps its traceback, and the
|
|
1822
|
+
traceback keeps the frames it passed through, with their locals, even
|
|
1823
|
+
after those functions returned (and, through each frame's caller, the
|
|
1824
|
+
frames below them). A runtime frame that still held the task there
|
|
1825
|
+
would tie the task, its exception and every object in those frames into
|
|
1826
|
+
a cycle that only the cyclic garbage collector frees. So the runtime's
|
|
1827
|
+
frames on such a path delete their references to the task (``del
|
|
1828
|
+
self``) before they raise or return.
|
|
1829
|
+
"""
|
|
1830
|
+
state = self._state
|
|
1831
|
+
if state is _SUCCEEDED:
|
|
1832
|
+
return None
|
|
1833
|
+
if state is _CANCELLED:
|
|
1834
|
+
return self._cancellation_exception()
|
|
1835
|
+
if state is _FAILED:
|
|
1836
|
+
return self._exception
|
|
1837
|
+
return InvalidStateError(f"{self.name} has not completed")
|
|
1777
1838
|
|
|
1778
1839
|
def _raise_or_return(self) -> T:
|
|
1779
1840
|
state = self._state
|
|
@@ -1917,7 +1978,16 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1917
1978
|
else:
|
|
1918
1979
|
self._sync_running = True
|
|
1919
1980
|
try:
|
|
1920
|
-
|
|
1981
|
+
if _context_flow._notifications_enabled:
|
|
1982
|
+
value = _run_with_context(context, target, *args, **kwargs)
|
|
1983
|
+
else:
|
|
1984
|
+
# Inlined (see _drive): no extra frame between the
|
|
1985
|
+
# function and this one in a traceback.
|
|
1986
|
+
try:
|
|
1987
|
+
value = context.run(target, *args, **kwargs)
|
|
1988
|
+
finally:
|
|
1989
|
+
if _context_flow._notifications_enabled:
|
|
1990
|
+
_notify_context_exit(context)
|
|
1921
1991
|
finally:
|
|
1922
1992
|
self._sync_running = False
|
|
1923
1993
|
state.tail = True
|
|
@@ -1935,11 +2005,13 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1935
2005
|
state.tail = True
|
|
1936
2006
|
self._finish(_CANCELLED, exception=error)
|
|
1937
2007
|
self._execution_context = None # break the cycle with the context
|
|
2008
|
+
del self, context, target, args, kwargs, work # see _release_frame
|
|
1938
2009
|
return
|
|
1939
2010
|
except BaseException as error:
|
|
1940
2011
|
state.tail = True
|
|
1941
2012
|
self._finish(_FAILED, exception=error)
|
|
1942
2013
|
self._execution_context = None # break the cycle with the context
|
|
2014
|
+
del self, context, target, args, kwargs, work # see _release_frame
|
|
1943
2015
|
return
|
|
1944
2016
|
|
|
1945
2017
|
self._coroutine = coroutine
|
|
@@ -1947,7 +2019,10 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1947
2019
|
# A synchronous function that returned an awaitable after the
|
|
1948
2020
|
# shutdown sweep: it missed the cancellation request.
|
|
1949
2021
|
self._cancel_requested = True
|
|
1950
|
-
|
|
2022
|
+
try:
|
|
2023
|
+
self._drive()
|
|
2024
|
+
finally:
|
|
2025
|
+
del self, context, target, args, kwargs, work, coroutine, value # see _release_frame
|
|
1951
2026
|
|
|
1952
2027
|
def _drive(self) -> None:
|
|
1953
2028
|
"""Run coroutine steps until the task parks, yields or completes."""
|
|
@@ -1958,6 +2033,8 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
1958
2033
|
while True:
|
|
1959
2034
|
value: Any = None
|
|
1960
2035
|
error: BaseException | None = None
|
|
2036
|
+
# The previous step's operation references what the task waited on.
|
|
2037
|
+
yielded: Any = None
|
|
1961
2038
|
with lock:
|
|
1962
2039
|
coroutine = self._coroutine
|
|
1963
2040
|
alive = self._state is _PENDING
|
|
@@ -2008,20 +2085,25 @@ class Task(Generic[T], metaclass=_TaskMeta):
|
|
|
2008
2085
|
self._coroutine = None
|
|
2009
2086
|
self._finish(_SUCCEEDED, result=stop.value)
|
|
2010
2087
|
self._execution_context = None # break the cycle with the context
|
|
2088
|
+
del self, context, coroutine, value, error, yielded # see _release_frame
|
|
2011
2089
|
return
|
|
2012
2090
|
except OperationCanceledError as cancellation:
|
|
2013
2091
|
state.tail = True
|
|
2014
2092
|
self._coroutine = None
|
|
2015
2093
|
self._finish(_CANCELLED, exception=cancellation)
|
|
2016
2094
|
self._execution_context = None # break the cycle with the context
|
|
2095
|
+
del self, context, coroutine, value, error, yielded # see _release_frame
|
|
2017
2096
|
return
|
|
2018
2097
|
except BaseException as failure:
|
|
2019
2098
|
state.tail = True
|
|
2020
2099
|
self._coroutine = None
|
|
2021
2100
|
self._finish(_FAILED, exception=failure)
|
|
2022
2101
|
self._execution_context = None # break the cycle with the context
|
|
2102
|
+
del self, context, coroutine, value, error, yielded # see _release_frame
|
|
2023
2103
|
return
|
|
2024
2104
|
|
|
2105
|
+
# Delivered: a caught exception must not stay in this frame.
|
|
2106
|
+
value = error = None
|
|
2025
2107
|
state.tail = True
|
|
2026
2108
|
try:
|
|
2027
2109
|
resume = runtime._suspend(self, yielded)
|
|
@@ -3491,6 +3573,7 @@ class _Runtime:
|
|
|
3491
3573
|
_close_if_coroutine(work)
|
|
3492
3574
|
raise
|
|
3493
3575
|
task._run_inline(state)
|
|
3576
|
+
del task, work, args, kwargs # see _release_frame
|
|
3494
3577
|
|
|
3495
3578
|
def _submit_long_running(
|
|
3496
3579
|
self, task: Task[Any], work: Any, args: tuple[Any, ...], kwargs: dict[str, Any]
|
|
@@ -3756,24 +3839,36 @@ class _Runtime:
|
|
|
3756
3839
|
|
|
3757
3840
|
|
|
3758
3841
|
def _queue_work_item(callback: Any, state: Any, *, flow_context: bool) -> bool:
|
|
3759
|
-
"""``ThreadPool.queue`` / ``queue_user_work_item``: queue ``callback`` without a Task.
|
|
3842
|
+
"""``ThreadPool.queue`` / ``queue_user_work_item``: queue ``callback`` without a Task.
|
|
3843
|
+
|
|
3844
|
+
``ThreadPool.queue`` has its own inlined copy of the ``flow_context=False`` path:
|
|
3845
|
+
it runs on a server's dispatch thread for every request.
|
|
3846
|
+
"""
|
|
3760
3847
|
if callback is None:
|
|
3761
3848
|
raise ArgumentNullError("callBack")
|
|
3762
3849
|
if not callable(callback):
|
|
3763
3850
|
raise TypeError("callback must be callable")
|
|
3764
3851
|
runtime = _get_runtime()
|
|
3852
|
+
context: Context | None = None
|
|
3765
3853
|
if flow_context:
|
|
3766
3854
|
context = copy_context()
|
|
3767
3855
|
if context.get(_current_task._value) is not None:
|
|
3768
3856
|
# A work item is not part of the task that queued it.
|
|
3769
3857
|
context.run(_current_task._value.set, None)
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3858
|
+
if runtime._shutdown_requested or not runtime._pool.queue(
|
|
3859
|
+
functools.partial(_run_work_item, runtime, callback, state, context)
|
|
3860
|
+
):
|
|
3861
|
+
raise RuntimeShutdownError("Task runtime is shutting down")
|
|
3773
3862
|
return True
|
|
3774
3863
|
|
|
3775
3864
|
|
|
3776
|
-
def _run_work_item(
|
|
3865
|
+
def _run_work_item(
|
|
3866
|
+
runtime: _Runtime, callback: Callable[..., Any], state: Any, context: Context | None = None
|
|
3867
|
+
) -> None:
|
|
3868
|
+
"""Run a work item; without a context of the caller's, in a fresh, empty one."""
|
|
3869
|
+
if context is None:
|
|
3870
|
+
# Created here, not on the queuing thread: one allocation less there.
|
|
3871
|
+
context = Context()
|
|
3777
3872
|
try:
|
|
3778
3873
|
if state is _NO_STATE:
|
|
3779
3874
|
_run_with_context(context, callback)
|
flpit/threading.py
CHANGED
|
@@ -8,6 +8,7 @@ import time as _time
|
|
|
8
8
|
from collections.abc import Callable
|
|
9
9
|
from contextvars import Context, ContextVar
|
|
10
10
|
from dataclasses import dataclass
|
|
11
|
+
from functools import partial as _partial
|
|
11
12
|
from datetime import timedelta
|
|
12
13
|
from typing import TYPE_CHECKING, Any, Final, cast, overload
|
|
13
14
|
|
|
@@ -434,7 +435,20 @@ class ThreadPool:
|
|
|
434
435
|
shared queue. Returns ``True``. Raises ``RuntimeShutdownError`` while the runtime
|
|
435
436
|
is shutting down.
|
|
436
437
|
"""
|
|
437
|
-
|
|
438
|
+
# _task._queue_work_item(callback, state, flow_context=False), inlined: a
|
|
439
|
+
# server's dispatch thread calls this for every request.
|
|
440
|
+
if callback is None:
|
|
441
|
+
raise ArgumentNullError("callBack")
|
|
442
|
+
if not callable(callback):
|
|
443
|
+
raise TypeError("callback must be callable")
|
|
444
|
+
runtime = _task._runtime
|
|
445
|
+
if runtime is None or runtime._closed:
|
|
446
|
+
runtime = _task._get_runtime()
|
|
447
|
+
if runtime._shutdown_requested or not runtime._pool.queue(
|
|
448
|
+
_partial(_task._run_work_item, runtime, callback, state)
|
|
449
|
+
):
|
|
450
|
+
raise _task.RuntimeShutdownError("Task runtime is shutting down")
|
|
451
|
+
return True
|
|
438
452
|
|
|
439
453
|
@overload
|
|
440
454
|
@staticmethod
|
|
@@ -5,8 +5,8 @@ flpit/concurrent.py,sha256=HZueI_zSTX9ozwmxwrn7_HSrxT4VL3CKoDbKQRqAKLA,32211
|
|
|
5
5
|
flpit/csv.py,sha256=QMG8AxYR32i4I_lD1KL284OXdScN6vjjgusvnqw2q-A,25862
|
|
6
6
|
flpit/flp.py,sha256=WaZnrtYdxwEOJUEBv_hXg8Uwm81h8kcH64pjDh8oJhU,1527
|
|
7
7
|
flpit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
flpit/task.py,sha256=
|
|
9
|
-
flpit/threading.py,sha256=
|
|
8
|
+
flpit/task.py,sha256=K04FsHTi5laq2z_mqaiDNfUbv9RrAXQU527z-lzIwj8,166426
|
|
9
|
+
flpit/threading.py,sha256=MeWwOI2DSejmwiivSL1IVGx0US-FEsgcNRjFWdwdcSg,37236
|
|
10
10
|
flpit/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
11
|
flpit/core/_grouping.py,sha256=QZNdNqXx4fcRsnun4nwkGKnst63Y10Q-rYBevof7Abw,954
|
|
12
12
|
flpit/core/_index.py,sha256=wewI5ykfVDPBMMzMgqM8UXdbwSgHwXw_E2Vf04QFVI0,398
|
|
@@ -20,7 +20,7 @@ flpit/json/__init__.py,sha256=7U7-KaxLbQybqmNTqDf4U54Njhac2ep5gxPEaavR7hA,4625
|
|
|
20
20
|
flpit/json/_core.py,sha256=IhLM116xW4jI4gg3-QmRGptVr5pHRY7X7aicm8TmO3Y,13445
|
|
21
21
|
flpit/json/_msgspec.py,sha256=19NB98R1tftz8LYY_2QriWo9d4Syii4f7QW_s8vWL2s,20124
|
|
22
22
|
flpit/json/_native.py,sha256=E_Ro3rXTCKDpb6jicBOYU9fzOJch6ZJkLqsKJNXAsTg,39974
|
|
23
|
-
flpit-0.4.2.
|
|
24
|
-
flpit-0.4.2.
|
|
25
|
-
flpit-0.4.2.
|
|
26
|
-
flpit-0.4.2.
|
|
23
|
+
flpit-0.4.2.dev5.dist-info/METADATA,sha256=PBv5XwNxGKZ1Ehx5DqTN6gTvUrt8uMxBiX9HQhpaCjQ,19395
|
|
24
|
+
flpit-0.4.2.dev5.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
25
|
+
flpit-0.4.2.dev5.dist-info/licenses/LICENSE,sha256=bi5Vg_YP3Oq3jaOMQT4b7WZu0NfBcZr-nRyOExUKJrQ,1063
|
|
26
|
+
flpit-0.4.2.dev5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|