Debian Codegraph @

src/trio/_core/_generated_io_epoll.py

raw
1 # ***********************************************************
2 # ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
3 # *************************************************************
4 from __future__ import annotations
5
6 import sys
7 from typing import TYPE_CHECKING
8
9 from ._ki import enable_ki_protection
10 from ._run import GLOBAL_RUN_CONTEXT
11
12 if TYPE_CHECKING:
13 from .._file_io import _HasFileNo
14
15 assert not TYPE_CHECKING or sys.platform == "linux"
16
17
18 __all__ = ["notify_closing", "wait_readable", "wait_writable"]
19
20
21 @enable_ki_protection
22 async def wait_readable(fd: int | _HasFileNo) -> None:
23 """Block until the kernel reports that the given object is readable.
24
25 On Unix systems, ``fd`` must either be an integer file descriptor,
26 or else an object with a ``.fileno()`` method which returns an
27 integer file descriptor. Any kind of file descriptor can be passed,
28 though the exact semantics will depend on your kernel. For example,
29 this probably won't do anything useful for on-disk files.
30
31 On Windows systems, ``fd`` must either be an integer ``SOCKET``
32 handle, or else an object with a ``.fileno()`` method which returns
33 an integer ``SOCKET`` handle. File descriptors aren't supported,
34 and neither are handles that refer to anything besides a
35 ``SOCKET``.
36
37 :raises trio.BusyResourceError:
38 if another task is already waiting for the given socket to
39 become readable.
40 :raises trio.ClosedResourceError:
41 if another task calls :func:`notify_closing` while this
42 function is still working.
43 """
44 try:
45 return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(fd)
46 except AttributeError:
47 raise RuntimeError("must be called from async context") from None
48
49
50 @enable_ki_protection
51 async def wait_writable(fd: int | _HasFileNo) -> None:
52 """Block until the kernel reports that the given object is writable.
53
54 See `wait_readable` for the definition of ``fd``.
55
56 :raises trio.BusyResourceError:
57 if another task is already waiting for the given socket to
58 become writable.
59 :raises trio.ClosedResourceError:
60 if another task calls :func:`notify_closing` while this
61 function is still working.
62 """
63 try:
64 return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(fd)
65 except AttributeError:
66 raise RuntimeError("must be called from async context") from None
67
68
69 @enable_ki_protection
70 def notify_closing(fd: int | _HasFileNo) -> None:
71 """Notify waiters of the given object that it will be closed.
72
73 Call this before closing a file descriptor (on Unix) or socket (on
74 Windows). This will cause any `wait_readable` or `wait_writable`
75 calls on the given object to immediately wake up and raise
76 `~trio.ClosedResourceError`.
77
78 This doesn't actually close the object – you still have to do that
79 yourself afterwards. Also, you want to be careful to make sure no
80 new tasks start waiting on the object in between when you call this
81 and when it's actually closed. So to close something properly, you
82 usually want to do these steps in order:
83
84 1. Explicitly mark the object as closed, so that any new attempts
85 to use it will abort before they start.
86 2. Call `notify_closing` to wake up any already-existing users.
87 3. Actually close the object.
88
89 It's also possible to do them in a different order if that's more
90 convenient, *but only if* you make sure not to have any checkpoints in
91 between the steps. This way they all happen in a single atomic
92 step, so other tasks won't be able to tell what order they happened
93 in anyway.
94 """
95 try:
96 return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(fd)
97 except AttributeError:
98 raise RuntimeError("must be called from async context") from None