Archive Tags RSS About
10 Sep 2020

Concurrent

Today, I learn python concurrent from python3 cookbook.

Bellow is some exampels using concurrent.

submit

import concurrent.futures

# Some function
do = [[1, 2, 3], [4, 5, 6]]

def work(x):
    print(f"x: {x}")
    return x

with concurrent.futures.ProcessPoolExecutor() as pool:
    # Example of submitting work to the pool
    future_result = pool.submit(work, do)

    # Obtaining the result (blocks until done)
    r = future_result.result()
    print(f"r: {r}")
x: [[1, 2, 3], [4, 5, 6]]
r: [[1, 2, 3], [4, 5, 6]]

map

with concurrent.futures.ProcessPoolExecutor() as pool:
    # Example of submitting work to the pool
    future_result = []
    for i in pool.map(work, do):
        future_result.append(i)

    # Obtaining the result (blocks until done)
    print(f"r: {future_result}")
x: [1, 2, 3]
x: [4, 5, 6]
r: [[1, 2, 3], [4, 5, 6]]
Tags: Concurrent
Creative Commons License
ZJ Org Blog by Jie Zhu is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.