Does Python asyncio use a thread pool?

I wrote this bit of code:

import asyncio
import threading
from aiohttp import ClientSession

async def fetch(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            response = await response.read()
            print(threading.current_thread().name)


loop = asyncio.get_event_loop()

tasks = [asyncio.ensure_future(fetch("http://example.com")) for i in range(5)]

loop.run_until_complete(asyncio.wait(tasks))

      

It prints "MainThread" every time. Does this mean that all requests are executed concurrently using the same main thread and do not use threads from the thread pool to execute each request, or are threads abstracted away?

This post seems to indicate that there is actually a thread pool that Python uses for these async tasks: http://skipperkongen.dk/2016/09/09/easy-parallel-http-requests-with-python-and -asyncio /

+3


source to share


1 answer


It prints "MainThread" every time. Does this mean that all requests are executed concurrently using the same main thread and do not use threads from the thread pool to execute each request, or are threads abstracted away?

Without using worker threads, asyncio

will only use the main thread. Concurrency is achieved through collaborative multitasking using Python generators (read coroutines ).



This message seems to indicate that there is actually a thread pool ...

Just like a module asyncio

, a blog post you explicitly link uses a module concurrent.futures

. The class ThreadPoolExecutor

from this code will spawn worker threads. But the sample code in your question will not.

+4


source







All Articles