Книга: Learning Concurrency in Python
Назад: Creating a ProcessPoolExecutor
Дальше: Output

Example

The following example features a very simple full example of how you can instantiate your own ProcessPoolExecutor and submit a couple of tasks into this pool. It should be noted that our task function here isn't that computationally expensive, so we may not see the full benefit of using multiple processes, and it could, in fact, be significantly slower than your typical single-threaded process.

We'll use the os module to find the current PID of each of the tasks that we execute within our pool:

from concurrent.futures import ProcessPoolExecutor
import os

def task():
print("Executing our Task on Process {}".format(os.getpid()))

def main():
executor = ProcessPoolExecutor(max_workers=3)
task1 = executor.submit(task)
task2 = executor.submit(task)

if __name__ == '__main__':
main()
Назад: Creating a ProcessPoolExecutor
Дальше: Output