DEV Community

Lam Nguyen
Lam Nguyen

Posted on

Answer: Can concurrent.futures.Future be converted to asyncio.Future?

For the "concurrent future to asyncio future" part, here is an utility I use.

from typing import List, Any
from concurrent.futures.thread import ThreadPoolExecutor
import asyncio


class AsyncThreadPool(ThreadPoolExecutor):
    _futures: List[asyncio.Future]
    _loop: asyncio.AbstractEventLoop

    def __init__(self, max_workers=None):
        super().__init__(max_workers)
        self._futures = []

    def queue(self, fn):
        self._loop = asyncio.get_event_loop()
        fut = self._loop.create_future()
        self._futures.append(fut)
        self.submit(self._entry, fn,

Top comments (0)