File tree Expand file tree Collapse file tree 3 files changed +23
-8
lines changed
src/frequenz/channels/util Expand file tree Collapse file tree 3 files changed +23
-8
lines changed Original file line number Diff line number Diff line change 22
33## Summary
44
5- <!-- Here goes a general summary of what this release is about -->
6-
75## Upgrading
86
9- <!-- Here goes notes on how to upgrade from previous versions, including if there are any depractions and what they should be replaced with -->
10-
117## New Features
128
13- <!-- Here goes the main new features and examples or instructions on how to use them -->
9+ * Add method to stop ` Merge ` and ` MergeNamed ` .
1410
1511## Bug Fixes
16-
17- <!-- Here goes notable bug fixes that are worth a special mention or explanation -->
Original file line number Diff line number Diff line change @@ -24,6 +24,9 @@ class Merge(Receiver[T]):
2424 # do something with msg
2525 pass
2626 ```
27+
28+ When `merge` is no longer needed, then it should be stopped using
29+ `self.stop()` method. This will cleanup any internal pending async tasks.
2730 """
2831
2932 def __init__ (self , * args : Receiver [T ]) -> None :
@@ -44,6 +47,13 @@ def __del__(self) -> None:
4447 for task in self ._pending :
4548 task .cancel ()
4649
50+ async def stop (self ) -> None :
51+ """Stop the `Merge` instance and cleanup any pending tasks."""
52+ for task in self ._pending :
53+ task .cancel ()
54+ await asyncio .gather (* self ._pending , return_exceptions = True )
55+ self ._pending = set ()
56+
4757 async def ready (self ) -> None :
4858 """Wait until the receiver is ready with a value.
4959
Original file line number Diff line number Diff line change 1111
1212
1313class MergeNamed (Receiver [Tuple [str , T ]]):
14- """Merge messages coming from multiple named channels into a single stream."""
14+ """Merge messages coming from multiple named channels into a single stream.
15+
16+ When `MergeNamed` is no longer needed, then it should be stopped using
17+ `self.stop()` method. This will cleanup any internal pending async tasks.
18+ """
1519
1620 def __init__ (self , ** kwargs : Receiver [T ]) -> None :
1721 """Create a `MergeNamed` instance.
@@ -31,6 +35,13 @@ def __del__(self) -> None:
3135 for task in self ._pending :
3236 task .cancel ()
3337
38+ async def stop (self ) -> None :
39+ """Stop the `MergeNamed` instance and cleanup any pending tasks."""
40+ for task in self ._pending :
41+ task .cancel ()
42+ await asyncio .gather (* self ._pending , return_exceptions = True )
43+ self ._pending = set ()
44+
3445 async def ready (self ) -> None :
3546 """Wait until there's a message in any of the channels.
3647
You can’t perform that action at this time.
0 commit comments