flpit 0.4.2.dev4__tar.gz → 0.4.2.dev5__tar.gz

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.
Files changed (27) hide show
  1. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/PKG-INFO +1 -1
  2. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/pyproject.toml +1 -1
  3. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/task.py +110 -15
  4. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/threading.py +15 -1
  5. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/.gitignore +0 -0
  6. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/LICENSE +0 -0
  7. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/README.md +0 -0
  8. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flp/__init__.py +0 -0
  9. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/__init__.py +0 -0
  10. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/channels.py +0 -0
  11. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/concurrent.py +0 -0
  12. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/__init__.py +0 -0
  13. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/_grouping.py +0 -0
  14. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/_index.py +0 -0
  15. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/_join.py +0 -0
  16. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/_lookup.py +0 -0
  17. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/_zip.py +0 -0
  18. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/async_linq.py +0 -0
  19. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/comparers.py +0 -0
  20. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/core/linq.py +0 -0
  21. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/csv.py +0 -0
  22. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/flp.py +0 -0
  23. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/json/__init__.py +0 -0
  24. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/json/_core.py +0 -0
  25. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/json/_msgspec.py +0 -0
  26. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/json/_native.py +0 -0
  27. {flpit-0.4.2.dev4 → flpit-0.4.2.dev5}/src/flpit/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: flpit
3
- Version: 0.4.2.dev4
3
+ Version: 0.4.2.dev5
4
4
  Summary: Fluent LINQ for Python — flip your iterables
5
5
  Project-URL: Homepage, https://github.com/riemar/flpit
6
6
  Project-URL: Documentation, https://riemar.github.io/flpit/
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flpit"
3
- version = "0.4.2.dev4"
3
+ version = "0.4.2.dev5"
4
4
  description = "Fluent LINQ for Python — flip your iterables"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -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
- return task
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
- return self._raise_or_return()
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
- raise self._cancellation_exception()
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
- return self._raise_or_return()
1732
- result = yield _WaitTask(self._runtime, self)
1733
- return result
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
- result = yield _WaitTask(self._runtime, self)
1776
- return result
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
- value = _run_with_context(context, target, *args, **kwargs)
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
- self._drive()
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
- else:
3771
- context = Context()
3772
- runtime._post(functools.partial(_run_work_item, runtime, context, callback, state))
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(runtime: _Runtime, context: Context, callback: Callable[..., Any], state: Any) -> None:
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)
@@ -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
- return _task._queue_work_item(callback, state, flow_context=False)
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
File without changes
File without changes
File without changes
File without changes
File without changes