Utilizing an existing threadpool with asyncio.to_thread

Currently when using asyncio.to_thread, the method spawns a new thread to run the blocking task in. What if one had a fixed set of worker threads they’d like asyncio.to_thread to use to execute the blocking task, is there a way to do this?
I’d suggest we have something like
asyncio.to_thread(executor = None,func,*args,**kwargs)
Currently we can’t have the executor parameter at the end of the parameter list because of the keyword arguments, and having it at the beginning would break the existing API, I dont know what best way to go around this.
Or we can think of adding a new API such as asyncio.submit(thd_pool,func,*args,**kwarg)
Also, is this something worth looking at? Or we can just use workarounds?

Looking at the source for to_thread(), I am not even sure why it exists. It seems a very thin wrapper around loop.run_in_executor(). This also suggests that in fact it already does what you ask – it doesn’t (necessarily) create a new thread, it uses the default executor. Isn’t that exactly what you’re asking for?

As Guido said, to_thread already uses the current thread (or process) pool to get the thread to execute the function in. This will create the thread of the pool isn’t full, otherwise uses an existing thread.

You can set the default executor with loop.set_default_executor to choose a different thread (or process) pool.


It’s handy because I often forget to pass the current context. Not sure why it’s in its own file though.

I think I’ve got it now.