Runs a function in a ``concurrent.futures.Executor``. If ``executor`` is ``None``, the IO loop's default executor will be used. Use `functools.partial` to pass keyword arguments to ``func``. .. versionadded:: 5.0
(
self,
executor: Optional[concurrent.futures.Executor],
func: Callable[..., _T],
*args: Any
)
| 695 | ) |
| 696 | |
| 697 | def run_in_executor( |
| 698 | self, |
| 699 | executor: Optional[concurrent.futures.Executor], |
| 700 | func: Callable[..., _T], |
| 701 | *args: Any |
| 702 | ) -> Awaitable[_T]: |
| 703 | """Runs a function in a ``concurrent.futures.Executor``. If |
| 704 | ``executor`` is ``None``, the IO loop's default executor will be used. |
| 705 | |
| 706 | Use `functools.partial` to pass keyword arguments to ``func``. |
| 707 | |
| 708 | .. versionadded:: 5.0 |
| 709 | """ |
| 710 | if executor is None: |
| 711 | if not hasattr(self, "_executor"): |
| 712 | from tornado.process import cpu_count |
| 713 | |
| 714 | self._executor = concurrent.futures.ThreadPoolExecutor( |
| 715 | max_workers=(cpu_count() * 5) |
| 716 | ) # type: concurrent.futures.Executor |
| 717 | executor = self._executor |
| 718 | c_future = executor.submit(func, *args) |
| 719 | # Concurrent Futures are not usable with await. Wrap this in a |
| 720 | # Tornado Future instead, using self.add_future for thread-safety. |
| 721 | t_future = Future() # type: Future[_T] |
| 722 | self.add_future(c_future, lambda f: chain_future(f, t_future)) |
| 723 | return t_future |
| 724 | |
| 725 | def set_default_executor(self, executor: concurrent.futures.Executor) -> None: |
| 726 | """Sets the default executor to use with :meth:`run_in_executor`. |
no test coverage detected