flpit 0.4.2.dev5__tar.gz → 0.4.2.dev7__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.dev7}/PKG-INFO +1 -1
  2. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/pyproject.toml +1 -1
  3. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/task.py +81 -69
  4. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/.gitignore +0 -0
  5. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/LICENSE +0 -0
  6. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/README.md +0 -0
  7. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flp/__init__.py +0 -0
  8. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/__init__.py +0 -0
  9. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/channels.py +0 -0
  10. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/concurrent.py +0 -0
  11. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/__init__.py +0 -0
  12. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/_grouping.py +0 -0
  13. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/_index.py +0 -0
  14. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/_join.py +0 -0
  15. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/_lookup.py +0 -0
  16. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/_zip.py +0 -0
  17. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/async_linq.py +0 -0
  18. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/comparers.py +0 -0
  19. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/core/linq.py +0 -0
  20. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/csv.py +0 -0
  21. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/flp.py +0 -0
  22. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/json/__init__.py +0 -0
  23. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/json/_core.py +0 -0
  24. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/json/_msgspec.py +0 -0
  25. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/json/_native.py +0 -0
  26. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/src/flpit/py.typed +0 -0
  27. {flpit-0.4.2.dev5 → flpit-0.4.2.dev7}/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.dev7
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.dev7"
4
4
  description = "Fluent LINQ for Python — flip your iterables"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -253,23 +253,19 @@ class _TaskMeta(type):
253
253
 
254
254
  @property
255
255
  def asyncio_interop(cls: "_TaskMeta") -> bool:
256
- return (
257
- getattr(cls, "__await__")
258
- is getattr(cls, "_await_asyncio_interop")
259
- )
256
+ """Deprecated, always True: a Task can always be awaited inside asyncio coroutines."""
257
+ warnings.warn(_ASYNCIO_INTEROP_DEPRECATED, DeprecationWarning, stacklevel=2)
258
+ return True
260
259
 
261
260
  @asyncio_interop.setter
262
- def asyncio_interop(
263
- cls: "_TaskMeta",
264
- enabled: bool,
265
- ) -> None:
266
- implementation = getattr(
267
- cls,
268
- "_await_asyncio_interop"
269
- if enabled
270
- else "_await_pure_flpit_task",
271
- )
272
- setattr(cls, "__await__", implementation)
261
+ def asyncio_interop(cls: "_TaskMeta", enabled: bool) -> None:
262
+ warnings.warn(_ASYNCIO_INTEROP_DEPRECATED + " Setting it has no effect.", DeprecationWarning, stacklevel=2)
263
+
264
+
265
+ _ASYNCIO_INTEROP_DEPRECATED: Final = (
266
+ "Task.asyncio_interop is deprecated and will be removed in 0.5: awaiting a Task inside "
267
+ "asyncio coroutines always works, at no measurable cost to flpit awaits."
268
+ )
273
269
 
274
270
 
275
271
  @dataclass(frozen=True)
@@ -763,12 +759,51 @@ class _WaitTask(_Operation):
763
759
 
764
760
  def __repr__(self) -> str:
765
761
  return (
766
- "<flpit _WaitTask: this await operation requires the "
767
- "flpit scheduler; enable Task.asyncio_interop to await "
768
- "flpit Task from asyncio>"
762
+ "<flpit _WaitTask: awaiting a flpit Task needs the flpit scheduler "
763
+ "or an asyncio event loop running on this thread>"
769
764
  )
770
765
 
771
766
 
767
+ #: asyncio's ``_get_running_loop`` once asyncio is imported (C-accelerated; None instead of raising).
768
+ _asyncio_get_running_loop: Callable[[], Any] | None = None
769
+
770
+
771
+ def _asyncio_running_loop() -> Any:
772
+ """The asyncio event loop running on this thread, or None.
773
+
774
+ Costs one C call once asyncio is imported; before that no loop can be running, and a dict
775
+ lookup is all it takes. Unlike ``asyncio.get_running_loop()`` it never raises, so asking on
776
+ a flpit worker costs about 16 ns instead of ~150-190 ns for a raised and caught exception.
777
+ """
778
+ global _asyncio_get_running_loop
779
+ get_running_loop = _asyncio_get_running_loop
780
+ if get_running_loop is None:
781
+ events = sys.modules.get("asyncio.events")
782
+ if events is None:
783
+ return None
784
+ get_running_loop = _asyncio_get_running_loop = events._get_running_loop
785
+ return get_running_loop()
786
+
787
+
788
+ def _notify_asyncio_future(loop: Any, future: Any, completed: Task[Any]) -> None:
789
+ """Continuation of a Task awaited from asyncio: resolve ``future`` on its loop."""
790
+ try:
791
+ loop.call_soon_threadsafe(_complete_asyncio_future, future, completed)
792
+ except RuntimeError:
793
+ pass # the loop is closed: nobody is left to resume
794
+
795
+
796
+ def _complete_asyncio_future(future: Any, completed: Task[Any]) -> None:
797
+ """Runs on the asyncio loop: pass the task's outcome to the future its waiter awaits."""
798
+ if future.done():
799
+ return # the waiter was cancelled and the future with it
800
+ error = completed._error_to_raise()
801
+ if error is None:
802
+ future.set_result(completed._result)
803
+ else:
804
+ future.set_exception(error)
805
+
806
+
772
807
  def _resume_from(waiter: "Task[Any]", completed: "Task[Any]") -> None:
773
808
  """Store the outcome of ``completed`` as the next value sent into ``waiter``.
774
809
 
@@ -1751,7 +1786,18 @@ class Task(Generic[T], metaclass=_TaskMeta):
1751
1786
  return self._exception
1752
1787
  return OperationCanceledError(f"{self.name} was cancelled")
1753
1788
 
1754
- def _await_pure_flpit_task(self) -> Generator[Any, T, T]:
1789
+ def __await__(self) -> Generator[Any, T, T]:
1790
+ """Await the task from flpit code or from an asyncio coroutine.
1791
+
1792
+ Awaited by flpit, a pending task asks one question: is an asyncio loop running on this
1793
+ thread? That is one C call (asyncio's ``_get_running_loop``, which returns None instead of
1794
+ raising), or a dict lookup while asyncio isn't imported at all (see _asyncio_running_loop);
1795
+ about 15 ns, within measurement noise of an await.
1796
+
1797
+ Inside asyncio, the task's completion resolves an asyncio future on that loop. Neither the
1798
+ frame nor the callbacks keep the task (see _release_frame): a failed task awaited from
1799
+ asyncio is freed by reference counting too.
1800
+ """
1755
1801
  if self._state is not _PENDING:
1756
1802
  error = self._error_to_raise()
1757
1803
  if error is None:
@@ -1761,59 +1807,25 @@ class Task(Generic[T], metaclass=_TaskMeta):
1761
1807
  raise error
1762
1808
  finally:
1763
1809
  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
1770
-
1771
- __await__ = _await_pure_flpit_task
1772
-
1773
- def _await_asyncio_interop(self) -> Generator[Any, T, T]:
1774
- if self._state is not _PENDING:
1775
- return self._raise_or_return()
1776
-
1777
- if "asyncio" in sys.modules:
1778
- import asyncio
1779
-
1810
+ loop = _asyncio_running_loop()
1811
+ if loop is None:
1812
+ operation = _WaitTask(self._runtime, self)
1813
+ del self # see _release_frame
1780
1814
  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
1815
+ return (yield operation)
1816
+ finally:
1817
+ del operation
1818
+ future = loop.create_future()
1819
+ # No closures: the task's continuation must stay valid after this frame is gone
1820
+ # (an asyncio waiter can be cancelled while the task still runs).
1821
+ notify = functools.partial(_notify_asyncio_future, loop, future)
1822
+ if not self._add_continuation(notify):
1823
+ notify(self)
1824
+ del self, notify # see _release_frame
1813
1825
  try:
1814
- return (yield operation)
1826
+ return (yield from cast(Generator[Any, T, T], future.__await__()))
1815
1827
  finally:
1816
- del operation
1828
+ del future
1817
1829
 
1818
1830
  def _error_to_raise(self) -> BaseException | None:
1819
1831
  """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