taskwire 0.3.0__tar.gz → 0.3.2__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 (62) hide show
  1. {taskwire-0.3.0 → taskwire-0.3.2}/PKG-INFO +1 -1
  2. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/__init__.py +1 -1
  3. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/calls.py +6 -6
  4. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/redis_store.py +41 -10
  5. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/reporter.py +12 -0
  6. {taskwire-0.3.0 → taskwire-0.3.2}/.gitignore +0 -0
  7. {taskwire-0.3.0 → taskwire-0.3.2}/LICENSE +0 -0
  8. {taskwire-0.3.0 → taskwire-0.3.2}/README.md +0 -0
  9. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/README.md +0 -0
  10. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/sequences/.gitkeep +0 -0
  11. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/aggregate-counts.json +0 -0
  12. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/aggregate-waiting-input-dominates.json +0 -0
  13. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/dialog-request-open.json +0 -0
  14. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/envelope-cancel.json +0 -0
  15. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/envelope-progress.json +0 -0
  16. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/envelope-snapshot.json +0 -0
  17. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/error-dispatch-failed.json +0 -0
  18. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-percent-absent.json +0 -0
  19. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-percent-null.json +0 -0
  20. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-private.json +0 -0
  21. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-shared-origins.json +0 -0
  22. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-cancelled.json +0 -0
  23. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-done.json +0 -0
  24. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-failed.json +0 -0
  25. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-queued.json +0 -0
  26. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-running.json +0 -0
  27. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/progress-state-waiting-input.json +0 -0
  28. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/register-empty-null-aggregate.json +0 -0
  29. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/register-v1-with-aggregate.json +0 -0
  30. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/result-kind-and-params.json +0 -0
  31. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/result-label-and-value-no-ref.json +0 -0
  32. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/result-ref-with-expires-at.json +0 -0
  33. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/snapshot-v1-wall-clock-rev.json +0 -0
  34. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/snapshot-waiting-input-uncollected-result.json +0 -0
  35. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/text-key-and-text.json +0 -0
  36. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/text-key-only.json +0 -0
  37. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/text-key-params-and-text.json +0 -0
  38. {taskwire-0.3.0 → taskwire-0.3.2}/conformance/wire/text-text-only.json +0 -0
  39. {taskwire-0.3.0 → taskwire-0.3.2}/pyproject.toml +0 -0
  40. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/ambient.py +0 -0
  41. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/conformance.py +0 -0
  42. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/__init__.py +0 -0
  43. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/asgi.py +0 -0
  44. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/celery.py +0 -0
  45. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/fastapi.py +0 -0
  46. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/muxws.py +0 -0
  47. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/schema.py +0 -0
  48. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/threads.py +0 -0
  49. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/viewsets.py +0 -0
  50. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/viewsets_events.py +0 -0
  51. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/contrib/wsgi.py +0 -0
  52. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/decorators.py +0 -0
  53. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/delivery.py +0 -0
  54. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/headers.py +0 -0
  55. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/models.py +0 -0
  56. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/py.typed +0 -0
  57. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/reader.py +0 -0
  58. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/register.py +0 -0
  59. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/rest.py +0 -0
  60. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/settings.py +0 -0
  61. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/store.py +0 -0
  62. {taskwire-0.3.0 → taskwire-0.3.2}/taskwire/transport.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: taskwire
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Progress reporting and awaitable dialogs for long-running server-side operations.
5
5
  Project-URL: Homepage, https://github.com/velis74/taskwire
6
6
  Project-URL: Repository, https://github.com/velis74/taskwire
@@ -12,7 +12,7 @@ outside. Nothing in the core imports a framework, a driver or a UI library (TW-C
12
12
 
13
13
  from __future__ import annotations
14
14
 
15
- __version__ = "0.3.0"
15
+ __version__ = "0.3.2"
16
16
 
17
17
  from .ambient import ask, current_reporter, progress
18
18
  from .calls import Call, call_token, end_call, execute_call, hand_off, receive_call
@@ -190,12 +190,12 @@ _execution: contextvars.ContextVar[CallExecution | None] = contextvars.ContextVa
190
190
 
191
191
 
192
192
  def _run(coroutine: Any) -> Any:
193
- """Run `coroutine` to completion on a loop of its own, for the synchronous forms."""
194
- loop = asyncio.new_event_loop()
195
- try:
196
- return loop.run_until_complete(coroutine)
197
- finally:
198
- loop.close()
193
+ """Run `coroutine` to completion on a loop of its own, for the synchronous forms.
194
+
195
+ `asyncio.run()` cancels the loop's remaining tasks before it closes it, which is what closes the
196
+ loop's client in a `RedisStore`.
197
+ """
198
+ return asyncio.run(coroutine)
199
199
 
200
200
 
201
201
  class CallReceipt:
@@ -31,7 +31,7 @@ import asyncio
31
31
  import contextlib
32
32
  import json
33
33
  import logging
34
- import weakref
34
+ import threading
35
35
 
36
36
  from typing import Any
37
37
 
@@ -269,24 +269,54 @@ class RedisStore(TaskwireStore):
269
269
 
270
270
  A `redis.asyncio` connection belongs to the event loop it was opened on. Built from `url`, the
271
271
  store opens one client per event loop that uses it, so one store serves every loop of a process:
272
- a worker's executions, its own transition loop, and the private loop of each synchronous
273
- `operation()`. A `client` passed in is used on every loop as it is, and is the caller's to keep
274
- on one.
272
+ a worker's executions, its own transition loop, the private loop of each synchronous
273
+ `operation()` and the loop of each `asyncio.run()` or `async_to_sync()`. A loop's client is
274
+ closed and forgotten when the loop's tasks are cancelled at its end, as `asyncio.run()` and the
275
+ synchronous `operation()` do. The client of a loop closed without that is forgotten by the next
276
+ call that opens a client, and its connections close when it is garbage-collected. A `client`
277
+ passed in is used on every loop as it is, and is the caller's to keep on one.
275
278
  """
276
279
  self._url = url
277
280
  self._fixed = _Scripts(client) if client is not None else None
278
- self._per_loop: weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, _Scripts] = weakref.WeakKeyDictionary()
281
+ self._per_loop: dict[asyncio.AbstractEventLoop, _Scripts] = {}
282
+ self._lock = threading.Lock()
279
283
 
280
284
  def _bound(self) -> _Scripts:
281
285
  if self._fixed is not None:
282
286
  return self._fixed
283
287
  loop = asyncio.get_running_loop()
284
- bound = self._per_loop.get(loop)
285
- if bound is None:
286
- bound = _Scripts(aioredis.Redis.from_url(self._url, decode_responses=True))
287
- self._per_loop[loop] = bound
288
+ with self._lock:
289
+ bound = self._per_loop.get(loop)
290
+ if bound is not None:
291
+ return bound
292
+ for closed in [held for held in self._per_loop if held.is_closed()]:
293
+ del self._per_loop[closed]
294
+ bound = self._per_loop[loop] = _Scripts(aioredis.Redis.from_url(self._url, decode_responses=True))
295
+ closer = loop.create_task(self._close_with_loop(loop, bound), name="RedisStore: close with the loop")
296
+ # A loop that ends without cancelling its tasks destroys this one without a warning.
297
+ closer._log_destroy_pending = False # type: ignore[attr-defined]
288
298
  return bound
289
299
 
300
+ async def _close_with_loop(self, loop: asyncio.AbstractEventLoop, bound: _Scripts) -> None:
301
+ try:
302
+ await loop.create_future()
303
+ except asyncio.CancelledError:
304
+ if self._forget(loop, bound):
305
+ await bound.redis.aclose()
306
+ raise
307
+ except GeneratorExit:
308
+ # The task is destroyed with its loop: there is no loop to close the client on.
309
+ self._forget(loop, bound)
310
+ raise
311
+
312
+ def _forget(self, loop: asyncio.AbstractEventLoop, bound: _Scripts) -> bool:
313
+ """Drop `bound` as the client of `loop`; False when it no longer is (`aclose()` dropped it)."""
314
+ with self._lock:
315
+ if self._per_loop.get(loop) is not bound:
316
+ return False
317
+ del self._per_loop[loop]
318
+ return True
319
+
290
320
  @property
291
321
  def _redis(self) -> Any:
292
322
  """The client of the running event loop."""
@@ -564,7 +594,8 @@ class RedisStore(TaskwireStore):
564
594
  if self._fixed is not None:
565
595
  await self._fixed.redis.aclose()
566
596
  return
567
- bound = self._per_loop.pop(asyncio.get_running_loop(), None)
597
+ with self._lock:
598
+ bound = self._per_loop.pop(asyncio.get_running_loop(), None)
568
599
  if bound is not None:
569
600
  await bound.redis.aclose()
570
601
 
@@ -1010,6 +1010,7 @@ class OperationScope:
1010
1010
  loop, thread = self._loop, self._loop_thread
1011
1011
  self._loop, self._loop_thread = None, None
1012
1012
  if loop is not None:
1013
+ asyncio.run_coroutine_threadsafe(_cancel_remaining_tasks(), loop).result()
1013
1014
  loop.call_soon_threadsafe(loop.stop)
1014
1015
  if thread is not None:
1015
1016
  thread.join(timeout=5)
@@ -1018,6 +1019,17 @@ class OperationScope:
1018
1019
  return False
1019
1020
 
1020
1021
 
1022
+ async def _cancel_remaining_tasks() -> None:
1023
+ """Cancel every other task of the running loop and wait for them, as `asyncio.run()` does before
1024
+ it closes its loop. A `RedisStore` closes the loop's client on that cancellation.
1025
+ """
1026
+ current = asyncio.current_task()
1027
+ tasks = [task for task in asyncio.all_tasks() if task is not current]
1028
+ for task in tasks:
1029
+ task.cancel()
1030
+ await asyncio.gather(*tasks, return_exceptions=True)
1031
+
1032
+
1021
1033
  def _terminal_for(exc_type: Any, exc: Any) -> tuple[ProgressState, Error | None]:
1022
1034
  """TW-API-001: exactly one completion, and `cancelled` is not `failed` (TW-CANCEL-006).
1023
1035
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes