trio
trio copied to clipboard
Use Windows message-oriented pipes to create Channels
Relevant to #1773, #1767, #824. Maybe even #959 and #1208?
This PR leverages Windows message-oriented pipes to create objects of type Channel[Bytes]
(according to the current state of #1208), supplementing the existing Stream
/Channel[Byte]
classes. This came about as I was trying to find a way to asyncify multiprocessing.connection.Pipe
for #1781, however the implementation stands totally separate from that. The internals of Pipe
are very similar to asyncio.windows_utils.pipe
except that the underlying objects are message-oriented rather than byte-oriented, meaning senders dump a whole message to the OS, and if receivers haven't allocated a big enough buffer, they get an "error", which is really just an invitation to get the rest with another, bigger buffer.
Working towards providing an API for #824, in addition to the class constructors it might make sense to provide make_pipe_stream
and make_pipe_channel
from test_windows_pipes
, since it is quite easy to get a windows pipe handle that is in the wrong state. Or possibly add some checks into the class constructors to ensure the pipe is in the right configuration with SetNamedPipeHandleState
? Also, users will need clear instructions as to how to get one end of the pipe into a subprocess. (I personally have no idea, I've been letting multiprocessing
do its magic.)
TODO:
- [x] Write tests
- [ ] Expose public API?
- [ ] Write docs
- [ ] Write newsfragment
Codecov Report
Merging #1782 (0a4ba95) into master (58e9b80) will increase coverage by
0.05%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## master #1782 +/- ##
==========================================
+ Coverage 99.59% 99.64% +0.05%
==========================================
Files 114 114
Lines 14574 14739 +165
Branches 1110 1118 +8
==========================================
+ Hits 14515 14687 +172
+ Misses 42 36 -6
+ Partials 17 16 -1
Impacted Files | Coverage Δ | |
---|---|---|
trio/_core/_windows_cffi.py | 100.00% <100.00%> (ø) |
|
trio/_windows_pipes.py | 100.00% <100.00%> (ø) |
|
trio/tests/test_windows_pipes.py | 100.00% <100.00%> (ø) |
|
trio/testing/_check_streams.py | 99.33% <0.00%> (+0.66%) |
:arrow_up: |
trio/tests/test_socket.py | 100.00% <0.00%> (+0.81%) |
:arrow_up: |
I think this PR is ready for review! not sure if CI needs to be refreshed somehow...
Woke up in a cold sweat having dreamed of an obscure cancellation bug. I think this is a safe use of the shielded cancel scope, otherwise I'll have to expose a sync function to call ReadFile
in _windows_cffi