On this page
  1. asyncio.to_thread
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

asyncio.to_thread

Prev: asyncio.timeout_at | Up: asyncio
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.to_thread
Tags: #public #python3 #stdlib #asyncio


Import

import asyncio
from asyncio import to_thread

Signature & Overview

asyncio.to_thread(func, /, *args, **kwargs)

Asynchronously run function func in a separate thread.

Usage

import asyncio

def blocking_sum(n: int) -> int:
    return sum(range(n))

async def main():
    total = await asyncio.to_thread(blocking_sum, 100)
    assert total == 4950

asyncio.run(main())

Gotchas

REPL Inspect

from asyncio import to_thread
help(to_thread)

Linked from

Related notes