Post content
https://pymotw.com/3/asyncio/executors.html Combining Coroutines with Threads and Processes A lot of existing libraries are not ready to be used with #asyncio natively. They may block, or depend on concurrency features not available through the module. It is still possible to use those libraries in an application based on asyncio by using an #executor from #concurrent.futures to run the code either in a separate thread or a separate process. #Threads The #run_in_executor() method of the event loop takes an executor instance, a regular callable to invoke, and any arguments to be passed to the callable. It returns a Future that can be used to wait for the function to finish its work and return something. If no executor is passed in, a #ThreadPoolExecutor is created. This example explicitly creates an executor to limit the number of worker threads it will have available. #Processes A ProcessPoolExecutor works in much the same way, creating a set of worker #processes instead of threads. Using separate processes requires more system resources, but for computationally-intensive operations it can make sense to run a separate task on each CPU core. #learn