Relevant documentation: https://trio.readthedocs.io/en/latest/reference-core.html#synchronizing-and-communicating-between-tasks
Relevant code: https://github.com/python-trio/trio/blob/master/trio/_channel.py
The documentation mentions, in close() docs, that "Using with receive_channel: will close the channel object on leaving the with block". On the other hand, the following section of the docs demonstrating the interface exclusively uses async with receive_channel: where the close() docs used regular with.
As per the code link, the relevant classes only have __enter__ and __exit__, there's no __aenter__ or __aexit__. So why does the demonstration in the docs use only async with? If I use regular with in my code, do I run the risk of future api changes breaking my code or some such?
It seems this is to aid refactoring, and generally to promote loose coupling via dependency inversion.
The
producerandconsumerfunctions in the simple channel example from the docs do take an arbitrarySendChannelandReceiveChannelrespectively, not a memory channel specifically - admittedly it's hard to see given the lack of type annocations :-PThe documentation of
MemoryReceiveChannel.closethat you already quoted states explicitlySo you can use normal
withblocks and theclose()method just fine with memory channels, there is no risk of breaking API changes. But if you ever want to change or reuse your code to work with a different channel implementation, say, aDTLSChannelor a web socket, you will prefer to have usedasync within the first place.