Asynchronous Executors

suggest change

Note: Uses the Python 3.5+ async/await syntax

asyncio supports the use of Executor objects found in concurrent.futures for scheduling tasks asynchronously. Event loops have the function run_in_executor() which takes an Executor object, a Callable, and the Callable’s parameters.

Scheduling a task for an Executor

import asyncio
from concurrent.futures import ThreadPoolExecutor

def func(a, b):

Do time intensive stuff…

return a + b

async def main(loop):

executor = ThreadPoolExecutor() result = await loop.run_in_executor(executor, func, “Hello,”, “ world!”) print(result)

if __name__ == "__main__":

loop = asyncio.get_event_loop() loop.run_until_complete(main(loop))

Each event loop also has a “default” Executor slot that can be assigned to an Executor. To assign an Executor and schedule tasks from the loop you use the set_default_executor() method.

import asyncio
from concurrent.futures import ThreadPoolExecutor

def func(a, b):

Do time intensive stuff…

return a + b

async def main(loop):

NOTE: Using None as the first parameter designates the default Executor.

result = await loop.run_in_executor(None, func, “Hello,”, “ world!”) print(result)

if __name__ == "__main__":

loop = asyncio.get_event_loop() loop.set_default_executor(ThreadPoolExecutor()) loop.run_until_complete(main(loop))

There are two main types of Executor in concurrent.futures, the ThreadPoolExecutor and the ProcessPoolExecutor. The ThreadPoolExecutor contains a pool of threads which can either be manually set to a specific number of threads through the constructor or defaults to the number of cores on the machine times 5. The ThreadPoolExecutor uses the pool of threads to execute tasks assigned to it and is generally better at CPU-bound operations rather than I/O bound operations. Contrast that to the ProcessPoolExecutor which spawns a new process for each task assigned to it. The ProcessPoolExecutor can only take tasks and parameters that are picklable. The most common non-picklable tasks are the methods of objects. If you must schedule an object’s method as a task in an Executor you must use a ThreadPoolExecutor.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Asyncio Module:
* Asynchronous Executors

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
53 Asyncio Module
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve