Multithreaded non-blocking `Resolver` implementation. Requires the `concurrent.futures` package to be installed (available in the standard library since Python 3.2, installable with ``pip install futures`` in older versions). The thread pool size can be configured with:: R
| 494 | |
| 495 | |
| 496 | class ThreadedResolver(ExecutorResolver): |
| 497 | """Multithreaded non-blocking `Resolver` implementation. |
| 498 | |
| 499 | Requires the `concurrent.futures` package to be installed |
| 500 | (available in the standard library since Python 3.2, |
| 501 | installable with ``pip install futures`` in older versions). |
| 502 | |
| 503 | The thread pool size can be configured with:: |
| 504 | |
| 505 | Resolver.configure('tornado.netutil.ThreadedResolver', |
| 506 | num_threads=10) |
| 507 | |
| 508 | .. versionchanged:: 3.1 |
| 509 | All ``ThreadedResolvers`` share a single thread pool, whose |
| 510 | size is set by the first one to be created. |
| 511 | |
| 512 | .. deprecated:: 5.0 |
| 513 | The default `Resolver` now uses `.IOLoop.run_in_executor`; use that instead |
| 514 | of this class. |
| 515 | """ |
| 516 | |
| 517 | _threadpool = None # type: ignore |
| 518 | _threadpool_pid = None # type: int |
| 519 | |
| 520 | def initialize(self, num_threads: int = 10) -> None: # type: ignore |
| 521 | threadpool = ThreadedResolver._create_threadpool(num_threads) |
| 522 | super().initialize(executor=threadpool, close_executor=False) |
| 523 | |
| 524 | @classmethod |
| 525 | def _create_threadpool( |
| 526 | cls, num_threads: int |
| 527 | ) -> concurrent.futures.ThreadPoolExecutor: |
| 528 | pid = os.getpid() |
| 529 | if cls._threadpool_pid != pid: |
| 530 | # Threads cannot survive after a fork, so if our pid isn't what it |
| 531 | # was when we created the pool then delete it. |
| 532 | cls._threadpool = None |
| 533 | if cls._threadpool is None: |
| 534 | cls._threadpool = concurrent.futures.ThreadPoolExecutor(num_threads) |
| 535 | cls._threadpool_pid = pid |
| 536 | return cls._threadpool |
| 537 | |
| 538 | |
| 539 | class OverrideResolver(Resolver): |
no outgoing calls