@@ -56,6 +56,56 @@ Global statistics
56
56
.. autoclass :: RunStatistics()
57
57
58
58
59
+ .. _trio_contexts :
60
+
61
+ Checking for Trio
62
+ -----------------
63
+
64
+ If you want to interact with an active Trio run -- perhaps you need to
65
+ know the :func: `~trio.current_time ` or the
66
+ :func: `~trio.lowlevel.current_task ` -- then Trio needs to have certain
67
+ state available to it or else you will get a
68
+ ``RuntimeError("must be called from async context") ``.
69
+ This requires that you either be:
70
+
71
+ * indirectly inside (and on the same thread as) a call to
72
+ :func: `trio.run `, for run-level information such as the
73
+ :func: `~trio.current_time ` or :func: `~trio.lowlevel.current_clock `;
74
+ or
75
+
76
+ * indirectly inside a Trio task, for task-level information such as
77
+ the :func: `~trio.lowlevel.current_task ` or
78
+ :func: `~trio.current_effective_deadline `.
79
+
80
+ Internally, this state is provided by thread-local variables tracking
81
+ the current run and the current task. Sometimes, it's useful to know
82
+ in advance whether a call will fail or to have dynamic information for
83
+ safeguards against running something inside or outside Trio. To do so,
84
+ call :func: `trio.lowlevel.in_trio_run ` or
85
+ :func: `trio.lowlevel.in_trio_task `, which will provide answers
86
+ according to the following table.
87
+
88
+
89
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
90
+ | situation | :func: `trio.lowlevel.in_trio_run ` | :func: `trio.lowlevel.in_trio_task ` |
91
+ +========================================================+===================================+====================================+
92
+ | inside a Trio-flavored async function | `True ` | `True ` |
93
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
94
+ | in a thread without an active call to :func: `trio.run ` | `False ` | `False ` |
95
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
96
+ | in a guest run's host loop | `True ` | `False ` |
97
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
98
+ | inside an instrument call | `True ` | depends |
99
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
100
+ | in a thread created by :func: `trio.to_thread.run_sync ` | `False ` | `False ` |
101
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
102
+ | inside an abort function | `True ` | `True ` |
103
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
104
+
105
+ .. autofunction :: in_trio_run
106
+
107
+ .. autofunction :: in_trio_task
108
+
59
109
The current clock
60
110
-----------------
61
111
0 commit comments