Call a function in a thread and return its result as a coroutine. This uses either :func:`asyncio.to_thread` or :func:`twisted.internet.threads.deferToThread`, depending on whether asyncio support is available. .. versionadded:: 2.15.0
(
func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs
)
| 294 | |
| 295 | |
| 296 | async def run_in_thread( |
| 297 | func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs |
| 298 | ) -> _T: |
| 299 | """Call a function in a thread and return its result as a coroutine. |
| 300 | |
| 301 | This uses either :func:`asyncio.to_thread` or |
| 302 | :func:`twisted.internet.threads.deferToThread`, depending on whether |
| 303 | asyncio support is available. |
| 304 | |
| 305 | .. versionadded:: 2.15.0 |
| 306 | """ |
| 307 | if is_asyncio_available(): |
| 308 | return await asyncio.to_thread(func, *args, **kwargs) |
| 309 | |
| 310 | # circular import |
| 311 | from scrapy.utils.defer import maybe_deferred_to_future # noqa: PLC0415 |
| 312 | |
| 313 | return await maybe_deferred_to_future(deferToThread(func, *args, **kwargs)) |
no test coverage detected