flpit 0.4.2.dev2__tar.gz → 0.4.2.dev4__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.dev4}/PKG-INFO +1 -1
  2. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/pyproject.toml +1 -1
  3. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/task.py +49 -4
  4. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/.gitignore +0 -0
  5. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/LICENSE +0 -0
  6. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/README.md +0 -0
  7. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flp/__init__.py +0 -0
  8. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/__init__.py +0 -0
  9. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/channels.py +0 -0
  10. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/concurrent.py +0 -0
  11. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/__init__.py +0 -0
  12. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/_grouping.py +0 -0
  13. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/_index.py +0 -0
  14. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/_join.py +0 -0
  15. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/_lookup.py +0 -0
  16. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/_zip.py +0 -0
  17. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/async_linq.py +0 -0
  18. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/comparers.py +0 -0
  19. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/core/linq.py +0 -0
  20. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/csv.py +0 -0
  21. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/flp.py +0 -0
  22. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/json/__init__.py +0 -0
  23. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/json/_core.py +0 -0
  24. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/json/_msgspec.py +0 -0
  25. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/json/_native.py +0 -0
  26. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/src/flpit/py.typed +0 -0
  27. {flpit-0.4.2.dev2 → flpit-0.4.2.dev4}/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.dev4
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.dev4"
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,12 @@ 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: otherwise a finished task, its result and every
863
+ # AsyncLocal value set inside it would only be freed by the cyclic
864
+ # garbage collector. (A failed task still is: its exception's traceback
865
+ # reaches the runtime frames that ran it, as with any stored exception.)
859
866
  self._execution_context: Context | None = (
860
867
  copy_context() if capture_context else None
861
868
  )
@@ -1658,12 +1665,15 @@ class Task(Generic[T], metaclass=_TaskMeta):
1658
1665
  _notify_context_exit(context)
1659
1666
  except StopIteration as stop:
1660
1667
  self._finish(_SUCCEEDED, result=stop.value)
1668
+ self._execution_context = None # break the cycle with the context
1661
1669
  return
1662
1670
  except OperationCanceledError as cancellation:
1663
1671
  self._finish(_CANCELLED, exception=cancellation)
1672
+ self._execution_context = None # break the cycle with the context
1664
1673
  return
1665
1674
  except BaseException as failure:
1666
1675
  self._finish(_FAILED, exception=failure)
1676
+ self._execution_context = None # break the cycle with the context
1667
1677
  return
1668
1678
 
1669
1679
  # The first real suspension (or an await that _suspend resolves at once).
@@ -1919,14 +1929,17 @@ class Task(Generic[T], metaclass=_TaskMeta):
1919
1929
  coroutine = _await_any(value)
1920
1930
  else:
1921
1931
  self._finish(_SUCCEEDED, result=value)
1932
+ self._execution_context = None # break the cycle with the context
1922
1933
  return
1923
1934
  except OperationCanceledError as error:
1924
1935
  state.tail = True
1925
1936
  self._finish(_CANCELLED, exception=error)
1937
+ self._execution_context = None # break the cycle with the context
1926
1938
  return
1927
1939
  except BaseException as error:
1928
1940
  state.tail = True
1929
1941
  self._finish(_FAILED, exception=error)
1942
+ self._execution_context = None # break the cycle with the context
1930
1943
  return
1931
1944
 
1932
1945
  self._coroutine = coroutine
@@ -1994,16 +2007,19 @@ class Task(Generic[T], metaclass=_TaskMeta):
1994
2007
  state.tail = True
1995
2008
  self._coroutine = None
1996
2009
  self._finish(_SUCCEEDED, result=stop.value)
2010
+ self._execution_context = None # break the cycle with the context
1997
2011
  return
1998
2012
  except OperationCanceledError as cancellation:
1999
2013
  state.tail = True
2000
2014
  self._coroutine = None
2001
2015
  self._finish(_CANCELLED, exception=cancellation)
2016
+ self._execution_context = None # break the cycle with the context
2002
2017
  return
2003
2018
  except BaseException as failure:
2004
2019
  state.tail = True
2005
2020
  self._coroutine = None
2006
2021
  self._finish(_FAILED, exception=failure)
2022
+ self._execution_context = None # break the cycle with the context
2007
2023
  return
2008
2024
 
2009
2025
  state.tail = True
@@ -2960,8 +2976,9 @@ class _IOReactor:
2960
2976
  commands = self._commands
2961
2977
  failure: BaseException | None = None
2962
2978
  try:
2979
+ park = _park_interval(None)
2963
2980
  while not self._closing:
2964
- for key, mask in selector.select(None):
2981
+ for key, mask in selector.select(park):
2965
2982
  if key.data is _WAKE:
2966
2983
  try:
2967
2984
  while reader.recv(4096):
@@ -3017,6 +3034,27 @@ class _IOReactor:
3017
3034
  return not thread.is_alive()
3018
3035
 
3019
3036
 
3037
+ def _park_interval(longest: float | None) -> float | None:
3038
+ """How long a runtime thread may block in one go while it has nothing to do.
3039
+
3040
+ On free-threaded builds an object is freed by the thread that created it: when
3041
+ another thread drops the last reference, the object is queued to its creator,
3042
+ which frees it the next time it runs Python code. A thread blocked for a long
3043
+ time would keep such objects (and everything they reference) alive. So there,
3044
+ idle threads wake up regularly. With the GIL, objects are freed at once and
3045
+ ``longest`` is kept.
3046
+ """
3047
+ gil_enabled = getattr(sys, "_is_gil_enabled", None)
3048
+ if gil_enabled is None or gil_enabled():
3049
+ return longest
3050
+ return _FREE_THREADED_PARK if longest is None else min(longest, _FREE_THREADED_PARK)
3051
+
3052
+
3053
+ # Free-threaded builds: the longest an idle runtime thread blocks without running
3054
+ # Python code (see _park_interval). Each wake-up costs a few microseconds.
3055
+ _FREE_THREADED_PARK: Final = 0.5
3056
+
3057
+
3020
3058
  def _spin_count() -> int:
3021
3059
  """How long an idle worker polls for work before it parks.
3022
3060
 
@@ -3167,6 +3205,7 @@ class _WorkerPool:
3167
3205
  parker.acquire()
3168
3206
  searching = True # whoever started this thread counted it as such
3169
3207
  counted = True
3208
+ park = cast(float, _park_interval(self._IDLE_TIMEOUT))
3170
3209
  try:
3171
3210
  while True:
3172
3211
  item = state.slot
@@ -3222,7 +3261,13 @@ class _WorkerPool:
3222
3261
  searching = True
3223
3262
  continue
3224
3263
 
3225
- while not parker.acquire(timeout=self._IDLE_TIMEOUT):
3264
+ idle_since = time.monotonic()
3265
+ while not parker.acquire(timeout=park):
3266
+ # Waking up ran Python code: objects this thread
3267
+ # created and others released are freed now.
3268
+ if time.monotonic() - idle_since < self._IDLE_TIMEOUT:
3269
+ continue
3270
+ idle_since = time.monotonic()
3226
3271
  with lock:
3227
3272
  if parker not in self._parked:
3228
3273
  # A waker picked this worker just now.
File without changes
File without changes
File without changes
File without changes
File without changes