@@ -58,28 +58,48 @@ Global statistics
58
58
59
59
.. _trio_contexts :
60
60
61
- The current Trio context
62
- ------------------------
63
-
64
- There are two different types of contexts in :mod: `trio `. Here are the
65
- semantics presented as a handy table. Choose the right function for
66
- your needs.
67
-
68
- +---------------------------------+-----------------------------------+------------------------------------+
69
- | situation | :func: `trio.lowlevel.in_trio_run ` | :func: `trio.lowlevel.in_trio_task ` |
70
- +=================================+===================================+====================================+
71
- | inside a running async function | `True ` | `True ` |
72
- +---------------------------------+-----------------------------------+------------------------------------+
73
- | without a running Trio loop | `False ` | `False ` |
74
- +---------------------------------+-----------------------------------+------------------------------------+
75
- | in a guest run's host loop | `True ` | `False ` |
76
- +---------------------------------+-----------------------------------+------------------------------------+
77
- | inside an instrument call | depends | depends |
78
- +---------------------------------+-----------------------------------+------------------------------------+
79
- | :func: `trio.to_thread.run_sync ` | `False ` | `False ` |
80
- +---------------------------------+-----------------------------------+------------------------------------+
81
- | inside an abort function | `True ` | `True ` |
82
- +---------------------------------+-----------------------------------+------------------------------------+
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
+
75
+ * indirectly inside a Trio task, for task-level information such as
76
+ the :func: `~trio.lowlevel.current_task ` or
77
+ :func: `~trio.current_effective_deadline `.
78
+
79
+ Internally, this state is provided by thread-local variables tracking
80
+ the current run and the current task. Sometimes, it's useful to know
81
+ in advance whether a call will fail or to have dynamic information for
82
+ safeguards against running something inside or outside Trio. To do so,
83
+ call :func: `trio.lowlevel.in_trio_run ` or
84
+ :func: `trio.lowlevel.in_trio_task `, which will provide answers
85
+ according to the following table.
86
+
87
+
88
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
89
+ | situation | :func: `trio.lowlevel.in_trio_run ` | :func: `trio.lowlevel.in_trio_task ` |
90
+ +========================================================+===================================+====================================+
91
+ | inside a Trio-flavored async function | `True ` | `True ` |
92
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
93
+ | in a thread without an active call to :func: `trio.run ` | `False ` | `False ` |
94
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
95
+ | in a guest run's host loop | `True ` | `False ` |
96
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
97
+ | inside an instrument call | depends | depends |
98
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
99
+ | in a thread created by :func: `trio.to_thread.run_sync ` | `False ` | `False ` |
100
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
101
+ | inside an abort function | `True ` | `True ` |
102
+ +--------------------------------------------------------+-----------------------------------+------------------------------------+
83
103
84
104
.. autofunction :: in_trio_run
85
105
0 commit comments