flpit 0.4.2.dev2__tar.gz → 0.4.2.dev3__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.dev2 → flpit-0.4.2.dev3}/PKG-INFO +1 -1
  2. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/pyproject.toml +1 -1
  3. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/task.py +68 -4
  4. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/.gitignore +0 -0
  5. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/LICENSE +0 -0
  6. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/README.md +0 -0
  7. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flp/__init__.py +0 -0
  8. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/__init__.py +0 -0
  9. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/channels.py +0 -0
  10. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/concurrent.py +0 -0
  11. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/__init__.py +0 -0
  12. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/_grouping.py +0 -0
  13. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/_index.py +0 -0
  14. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/_join.py +0 -0
  15. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/_lookup.py +0 -0
  16. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/_zip.py +0 -0
  17. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/async_linq.py +0 -0
  18. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/comparers.py +0 -0
  19. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/core/linq.py +0 -0
  20. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/csv.py +0 -0
  21. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/flp.py +0 -0
  22. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/json/__init__.py +0 -0
  23. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/json/_core.py +0 -0
  24. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/json/_msgspec.py +0 -0
  25. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/json/_native.py +0 -0
  26. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/py.typed +0 -0
  27. {flpit-0.4.2.dev2 → flpit-0.4.2.dev3}/src/flpit/threading.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: flpit
3
- Version: 0.4.2.dev2
3
+ Version: 0.4.2.dev3
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.dev2"
3
+ version = "0.4.2.dev3"
4
4
  description = "Fluent LINQ for Python — flip your iterables"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -401,6 +401,7 @@ class _TimerQueue:
401
401
  _thread_state.internal = True
402
402
  condition = self._condition
403
403
  due: list[Callable[[], Any]] = []
404
+ park = cast(float, _park_interval(self._MAX_WAIT))
404
405
  while True:
405
406
  with condition:
406
407
  while True:
@@ -421,9 +422,9 @@ class _TimerQueue:
421
422
  if due:
422
423
  break
423
424
  if heap:
424
- condition.wait(min(heap[0][0] - now, self._MAX_WAIT))
425
+ condition.wait(min(heap[0][0] - now, park))
425
426
  else:
426
- condition.wait()
427
+ condition.wait(_park_interval(None))
427
428
  for callback in due:
428
429
  try:
429
430
  callback()
@@ -856,6 +857,11 @@ class Task(Generic[T], metaclass=_TaskMeta):
856
857
  self._lock = threading.Lock()
857
858
  self._state: str = _PENDING
858
859
  # Promise-style tasks never run code and need no execution context.
860
+ # The context holds this task as the current task, so the two form a
861
+ # reference cycle. Every path on which the task's code has finished
862
+ # drops the context ("see _release_context"): otherwise a finished
863
+ # task, its result and every AsyncLocal value set inside it would only
864
+ # be freed by the cyclic garbage collector.
859
865
  self._execution_context: Context | None = (
860
866
  copy_context() if capture_context else None
861
867
  )
@@ -1658,12 +1664,15 @@ class Task(Generic[T], metaclass=_TaskMeta):
1658
1664
  _notify_context_exit(context)
1659
1665
  except StopIteration as stop:
1660
1666
  self._finish(_SUCCEEDED, result=stop.value)
1667
+ self._execution_context = None # break the cycle with the context
1661
1668
  return
1662
1669
  except OperationCanceledError as cancellation:
1663
1670
  self._finish(_CANCELLED, exception=cancellation)
1671
+ self._execution_context = None # break the cycle with the context
1664
1672
  return
1665
1673
  except BaseException as failure:
1666
1674
  self._finish(_FAILED, exception=failure)
1675
+ self._execution_context = None # break the cycle with the context
1667
1676
  return
1668
1677
 
1669
1678
  # The first real suspension (or an await that _suspend resolves at once).
@@ -1795,6 +1804,9 @@ class Task(Generic[T], metaclass=_TaskMeta):
1795
1804
  if result is not None:
1796
1805
  self._result = result
1797
1806
  if exception is not None:
1807
+ tb = exception.__traceback__
1808
+ if tb is not None and tb.tb_frame.f_code in _RUNTIME_CODES:
1809
+ exception.__traceback__ = _without_runtime_frames(tb)
1798
1810
  self._exception = exception
1799
1811
  # Published last: lock-free readers check the state first.
1800
1812
  self._state = state
@@ -1919,14 +1931,17 @@ class Task(Generic[T], metaclass=_TaskMeta):
1919
1931
  coroutine = _await_any(value)
1920
1932
  else:
1921
1933
  self._finish(_SUCCEEDED, result=value)
1934
+ self._execution_context = None # break the cycle with the context
1922
1935
  return
1923
1936
  except OperationCanceledError as error:
1924
1937
  state.tail = True
1925
1938
  self._finish(_CANCELLED, exception=error)
1939
+ self._execution_context = None # break the cycle with the context
1926
1940
  return
1927
1941
  except BaseException as error:
1928
1942
  state.tail = True
1929
1943
  self._finish(_FAILED, exception=error)
1944
+ self._execution_context = None # break the cycle with the context
1930
1945
  return
1931
1946
 
1932
1947
  self._coroutine = coroutine
@@ -1994,16 +2009,19 @@ class Task(Generic[T], metaclass=_TaskMeta):
1994
2009
  state.tail = True
1995
2010
  self._coroutine = None
1996
2011
  self._finish(_SUCCEEDED, result=stop.value)
2012
+ self._execution_context = None # break the cycle with the context
1997
2013
  return
1998
2014
  except OperationCanceledError as cancellation:
1999
2015
  state.tail = True
2000
2016
  self._coroutine = None
2001
2017
  self._finish(_CANCELLED, exception=cancellation)
2018
+ self._execution_context = None # break the cycle with the context
2002
2019
  return
2003
2020
  except BaseException as failure:
2004
2021
  state.tail = True
2005
2022
  self._coroutine = None
2006
2023
  self._finish(_FAILED, exception=failure)
2024
+ self._execution_context = None # break the cycle with the context
2007
2025
  return
2008
2026
 
2009
2027
  state.tail = True
@@ -2093,6 +2111,23 @@ class Task(Generic[T], metaclass=_TaskMeta):
2093
2111
 
2094
2112
  _current_task: AsyncLocal[Task[Any] | None] = AsyncLocal()
2095
2113
 
2114
+ # The runtime's own frames, which head the traceback of an exception that escapes a
2115
+ # task's code. They tell the user nothing, and their locals include the task itself:
2116
+ # kept in the stored exception, they would tie the task, its exception and everything
2117
+ # the traceback references into a cycle that only the cyclic GC can free.
2118
+ _RUNTIME_CODES: Final = frozenset({
2119
+ Task._start.__code__,
2120
+ Task._drive.__code__,
2121
+ Task._run_coroutine_inline.__code__,
2122
+ _run_with_context.__code__,
2123
+ })
2124
+
2125
+
2126
+ def _without_runtime_frames(tb: Any) -> Any:
2127
+ while tb is not None and tb.tb_frame.f_code in _RUNTIME_CODES:
2128
+ tb = tb.tb_next
2129
+ return tb
2130
+
2096
2131
 
2097
2132
  # ---------------------------------------------------------------------------
2098
2133
  # Async generators (PEP 525 hooks)
@@ -2960,8 +2995,9 @@ class _IOReactor:
2960
2995
  commands = self._commands
2961
2996
  failure: BaseException | None = None
2962
2997
  try:
2998
+ park = _park_interval(None)
2963
2999
  while not self._closing:
2964
- for key, mask in selector.select(None):
3000
+ for key, mask in selector.select(park):
2965
3001
  if key.data is _WAKE:
2966
3002
  try:
2967
3003
  while reader.recv(4096):
@@ -3017,6 +3053,27 @@ class _IOReactor:
3017
3053
  return not thread.is_alive()
3018
3054
 
3019
3055
 
3056
+ def _park_interval(longest: float | None) -> float | None:
3057
+ """How long a runtime thread may block in one go while it has nothing to do.
3058
+
3059
+ On free-threaded builds an object is freed by the thread that created it: when
3060
+ another thread drops the last reference, the object is queued to its creator,
3061
+ which frees it the next time it runs Python code. A thread blocked for a long
3062
+ time would keep such objects (and everything they reference) alive. So there,
3063
+ idle threads wake up regularly. With the GIL, objects are freed at once and
3064
+ ``longest`` is kept.
3065
+ """
3066
+ gil_enabled = getattr(sys, "_is_gil_enabled", None)
3067
+ if gil_enabled is None or gil_enabled():
3068
+ return longest
3069
+ return _FREE_THREADED_PARK if longest is None else min(longest, _FREE_THREADED_PARK)
3070
+
3071
+
3072
+ # Free-threaded builds: the longest an idle runtime thread blocks without running
3073
+ # Python code (see _park_interval). Each wake-up costs a few microseconds.
3074
+ _FREE_THREADED_PARK: Final = 0.5
3075
+
3076
+
3020
3077
  def _spin_count() -> int:
3021
3078
  """How long an idle worker polls for work before it parks.
3022
3079
 
@@ -3167,6 +3224,7 @@ class _WorkerPool:
3167
3224
  parker.acquire()
3168
3225
  searching = True # whoever started this thread counted it as such
3169
3226
  counted = True
3227
+ park = cast(float, _park_interval(self._IDLE_TIMEOUT))
3170
3228
  try:
3171
3229
  while True:
3172
3230
  item = state.slot
@@ -3222,7 +3280,13 @@ class _WorkerPool:
3222
3280
  searching = True
3223
3281
  continue
3224
3282
 
3225
- while not parker.acquire(timeout=self._IDLE_TIMEOUT):
3283
+ idle_since = time.monotonic()
3284
+ while not parker.acquire(timeout=park):
3285
+ # Waking up ran Python code: objects this thread
3286
+ # created and others released are freed now.
3287
+ if time.monotonic() - idle_since < self._IDLE_TIMEOUT:
3288
+ continue
3289
+ idle_since = time.monotonic()
3226
3290
  with lock:
3227
3291
  if parker not in self._parked:
3228
3292
  # A waker picked this worker just now.
File without changes
File without changes
File without changes
File without changes
File without changes