Created
March 15, 2025 08:36
-
-
Save graingert/5c87332095d67ea3e1c882ebd9b3df25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import asyncio | |
import selectors | |
async def wait_readable(s): | |
event = asyncio.Event() | |
loop = asyncio.get_running_loop() | |
loop.add_reader(s, event.set) | |
try: | |
await event.wait() | |
finally: | |
loop.remove_reader(s) | |
async def main(): | |
class _Done(Exception): | |
pass | |
s1, s2 = socket.socketpair() | |
with s1, s2: | |
s1.setblocking(False) | |
s2.setblocking(False) | |
try: | |
async with asyncio.TaskGroup() as tg: | |
tg.create_task(wait_readable(s2)) | |
await asyncio.sleep(1) | |
s2.close() | |
await asyncio.sleep(1) | |
raise _Done | |
except* _Done: | |
pass | |
return False | |
s1, s2 = socket.socketpair() | |
with s1, s2: | |
s1.setblocking(False) | |
s2.setblocking(False) | |
s1.send(b"\x00") | |
await wait_readable(s2) | |
asyncio.run( | |
main(), | |
loop_factory=lambda: asyncio.SelectorEventLoop(selector=selectors.SelectSelector()), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment