Returns a global Context instance. Most single-process applications have a single, global Context. Use this method instead of passing around Context instances throughout your code. A common pattern for classes that depend on Contexts is to use a default argu
(cls: type[_ContextType], io_threads: int = 1)
| 205 | # static method copied from tornado IOLoop.instance |
| 206 | @classmethod |
| 207 | def instance(cls: type[_ContextType], io_threads: int = 1) -> _ContextType: |
| 208 | """Returns a global Context instance. |
| 209 | |
| 210 | Most single-process applications have a single, global Context. |
| 211 | Use this method instead of passing around Context instances |
| 212 | throughout your code. |
| 213 | |
| 214 | A common pattern for classes that depend on Contexts is to use |
| 215 | a default argument to enable programs with multiple Contexts |
| 216 | but not require the argument for simpler applications:: |
| 217 | |
| 218 | class MyClass(object): |
| 219 | def __init__(self, context=None): |
| 220 | self.context = context or Context.instance() |
| 221 | |
| 222 | .. versionchanged:: 18.1 |
| 223 | |
| 224 | When called in a subprocess after forking, |
| 225 | a new global instance is created instead of inheriting |
| 226 | a Context that won't work from the parent process. |
| 227 | """ |
| 228 | if ( |
| 229 | cls._instance is None |
| 230 | or cls._instance_pid != os.getpid() |
| 231 | or cls._instance.closed |
| 232 | ): |
| 233 | with cls._instance_lock: |
| 234 | if ( |
| 235 | cls._instance is None |
| 236 | or cls._instance_pid != os.getpid() |
| 237 | or cls._instance.closed |
| 238 | ): |
| 239 | cls._instance = cls(io_threads=io_threads) |
| 240 | cls._instance_pid = os.getpid() |
| 241 | return cls._instance |
| 242 | |
| 243 | def term(self) -> None: |
| 244 | """Close or terminate the context. |
no outgoing calls