trio
trio copied to clipboard
RFC: UNIX server sockets
Implement high level API for UNIX server sockets, based on https://github.com/python-trio/trio/issues/279
This needs some more work but I'd like to invite others for comments at this point.
Just tried to figure out how to serve a unix socket and ended up here, thanks for this effort ... I guess my only capacity to help this along is basically to bump the PR with this comment :sweat_smile: There is a whole bunch of stuff here I had no idea that should be taken care of when dealing with sockets...
Oh yeah, could also take it for a test drive! Saw the following warning when running serve_unix
:
TrioDeprecationWarning: subclassing trio.SocketListener is deprecated since Trio 0.15.0; use composition or delegation instead (https://github.com/python-trio/trio/issues/1044) class UnixSocketListener(trio.SocketListener):
Seems to do the job :sun_with_face:
from functools import partial
from trio import open_nursery, open_unix_socket, run
from trio_socket import serve_unix # gh:python-trio/trio/pull/1433
SOCK_PATH = "/tmp/temp.sock"
async def client():
stream = await open_unix_socket(SOCK_PATH)
async with stream:
await stream.send_all(b"hello, world")
async def server(stream):
async for data in stream:
print(f"received -> {data}")
async def main():
async with open_nursery() as server_nursery:
await server_nursery.start(partial(serve_unix, server, SOCK_PATH))
async with open_nursery() as client_nursery:
client_nursery.start_soon(client)
server_nursery.cancel_scope.cancel()
run(main)
I am also bumping this PR as I think it would be nice to have also UDS server support - thank you @Tronic + @decentral1se you saved me ton of time figuring this out
Codecov Report
Attention: 110 lines
in your changes are missing coverage. Please review.
Comparison is base (
78c55aa
) 92.94% compared to head (99f17c5
) 98.57%.
:exclamation: Current head 99f17c5 differs from pull request most recent head 8e613a0. Consider uploading reports for the commit 8e613a0 to get more accurate results
Additional details and impacted files
@@ Coverage Diff @@
## master #1433 +/- ##
==========================================
+ Coverage 92.94% 98.57% +5.63%
==========================================
Files 117 117
Lines 17634 17774 +140
Branches 3172 3174 +2
==========================================
+ Hits 16390 17521 +1131
+ Misses 1154 204 -950
+ Partials 90 49 -41
Files | Coverage Δ | |
---|---|---|
trio/__init__.py | 100.00% <100.00%> (ø) |
|
trio/_tests/test_highlevel_open_unix_listeners.py | 38.46% <38.46%> (ø) |
|
trio/_highlevel_open_unix_listeners.py | 30.88% <30.88%> (ø) |