flpit 0.4.2.dev5__tar.gz → 0.4.2.dev6__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.dev5 → flpit-0.4.2.dev6}/PKG-INFO +1 -1
  2. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/pyproject.toml +1 -1
  3. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/task.py +77 -38
  4. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/.gitignore +0 -0
  5. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/LICENSE +0 -0
  6. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/README.md +0 -0
  7. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flp/__init__.py +0 -0
  8. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/__init__.py +0 -0
  9. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/channels.py +0 -0
  10. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/concurrent.py +0 -0
  11. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/__init__.py +0 -0
  12. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/_grouping.py +0 -0
  13. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/_index.py +0 -0
  14. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/_join.py +0 -0
  15. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/_lookup.py +0 -0
  16. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/_zip.py +0 -0
  17. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/async_linq.py +0 -0
  18. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/comparers.py +0 -0
  19. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/core/linq.py +0 -0
  20. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/csv.py +0 -0
  21. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/flp.py +0 -0
  22. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/json/__init__.py +0 -0
  23. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/json/_core.py +0 -0
  24. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/json/_msgspec.py +0 -0
  25. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/json/_native.py +0 -0
  26. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/src/flpit/py.typed +0 -0
  27. {flpit-0.4.2.dev5 → flpit-0.4.2.dev6}/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.dev5
3
+ Version: 0.4.2.dev6
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.dev5"
3
+ version = "0.4.2.dev6"
4
4
  description = "Fluent LINQ for Python — flip your iterables"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -769,6 +769,46 @@ class _WaitTask(_Operation):
769
769
  )
770
770
 
771
771
 
772
+ #: asyncio's ``_get_running_loop`` once asyncio is imported (C-accelerated; None instead of raising).
773
+ _asyncio_get_running_loop: Callable[[], Any] | None = None
774
+
775
+
776
+ def _asyncio_running_loop() -> Any:
777
+ """The asyncio event loop running on this thread, or None.
778
+
779
+ Costs one C call once asyncio is imported; before that no loop can be running, and a dict
780
+ lookup is all it takes. Unlike ``asyncio.get_running_loop()`` it never raises, so asking on
781
+ a flpit worker costs about 16 ns instead of ~150-190 ns for a raised and caught exception.
782
+ """
783
+ global _asyncio_get_running_loop
784
+ get_running_loop = _asyncio_get_running_loop
785
+ if get_running_loop is None:
786
+ events = sys.modules.get("asyncio.events")
787
+ if events is None:
788
+ return None
789
+ get_running_loop = _asyncio_get_running_loop = events._get_running_loop
790
+ return get_running_loop()
791
+
792
+
793
+ def _notify_asyncio_future(loop: Any, future: Any, completed: Task[Any]) -> None:
794
+ """Continuation of a Task awaited from asyncio: resolve ``future`` on its loop."""
795
+ try:
796
+ loop.call_soon_threadsafe(_complete_asyncio_future, future, completed)
797
+ except RuntimeError:
798
+ pass # the loop is closed: nobody is left to resume
799
+
800
+
801
+ def _complete_asyncio_future(future: Any, completed: Task[Any]) -> None:
802
+ """Runs on the asyncio loop: pass the task's outcome to the future its waiter awaits."""
803
+ if future.done():
804
+ return # the waiter was cancelled and the future with it
805
+ error = completed._error_to_raise()
806
+ if error is None:
807
+ future.set_result(completed._result)
808
+ else:
809
+ future.set_exception(error)
810
+
811
+
772
812
  def _resume_from(waiter: "Task[Any]", completed: "Task[Any]") -> None:
773
813
  """Store the outcome of ``completed`` as the next value sent into ``waiter``.
774
814
 
@@ -1771,49 +1811,48 @@ class Task(Generic[T], metaclass=_TaskMeta):
1771
1811
  __await__ = _await_pure_flpit_task
1772
1812
 
1773
1813
  def _await_asyncio_interop(self) -> Generator[Any, T, T]:
1774
- if self._state is not _PENDING:
1775
- return self._raise_or_return()
1814
+ """``__await__`` that also works inside asyncio coroutines (``Task.asyncio_interop``).
1776
1815
 
1777
- if "asyncio" in sys.modules:
1778
- import asyncio
1816
+ Awaited by flpit, it is the pure path plus one question, asked only while the task is
1817
+ still pending: is an asyncio loop running on this thread? That is one C call (asyncio's
1818
+ ``_get_running_loop``, which returns None instead of raising), or a dict lookup while
1819
+ asyncio isn't imported at all (see _asyncio_running_loop).
1779
1820
 
1821
+ Inside asyncio, the task's completion resolves an asyncio future on that loop. Neither the
1822
+ frame nor the callbacks keep the task (see _release_frame): a failed task awaited from
1823
+ asyncio is freed by reference counting too.
1824
+ """
1825
+ if self._state is not _PENDING:
1826
+ error = self._error_to_raise()
1827
+ if error is None:
1828
+ return cast(T, self._result)
1829
+ del self # see _release_frame
1780
1830
  try:
1781
- loop = asyncio.get_running_loop()
1782
- except RuntimeError:
1783
- pass
1784
- else:
1785
- future: asyncio.Future[T] = loop.create_future()
1786
-
1787
- def complete_future() -> None:
1788
- if future.done():
1789
- return
1790
-
1791
- try:
1792
- result = self._raise_or_return()
1793
- except BaseException as exc:
1794
- future.set_exception(exc)
1795
- else:
1796
- future.set_result(result)
1797
-
1798
- def notify(_: Task[Any]) -> None:
1799
- try:
1800
- loop.call_soon_threadsafe(complete_future)
1801
- except RuntimeError:
1802
- pass
1803
-
1804
- if not self._add_continuation(notify):
1805
- notify(self)
1806
-
1807
- return (
1808
- yield from cast(Generator[Any, T, T], future.__await__())
1809
- )
1810
-
1811
- operation = _WaitTask(self._runtime, self)
1812
- del self # see _release_frame
1831
+ raise error
1832
+ finally:
1833
+ del error
1834
+ loop = _asyncio_running_loop()
1835
+ if loop is None:
1836
+ operation = _WaitTask(self._runtime, self)
1837
+ del self # see _release_frame
1838
+ try:
1839
+ return (yield operation)
1840
+ finally:
1841
+ del operation
1842
+ future = loop.create_future()
1843
+ # No closures: the task's continuation must stay valid after this frame is gone
1844
+ # (an asyncio waiter can be cancelled while the task still runs).
1845
+ notify = functools.partial(_notify_asyncio_future, loop, future)
1846
+ if not self._add_continuation(notify):
1847
+ notify(self)
1848
+ del self, notify # see _release_frame
1813
1849
  try:
1814
- return (yield operation)
1850
+ return (yield from cast(Generator[Any, T, T], future.__await__()))
1815
1851
  finally:
1816
- del operation
1852
+ del future
1853
+
1854
+ # On by default: awaited by flpit code it costs about 0.1 µs (see _await_asyncio_interop).
1855
+ __await__ = _await_asyncio_interop
1817
1856
 
1818
1857
  def _error_to_raise(self) -> BaseException | None:
1819
1858
  """The exception that awaiting this completed task raises; None if it succeeded.
File without changes
File without changes
File without changes
File without changes
File without changes