Joldnine / joldnine.github.io

My github.io blog repo. https://joldnine.github.io
2 stars 1 forks source link

Python: Submit multi parameters function to Executor #10

Open Joldnine opened 6 years ago

Joldnine commented 6 years ago

In Python, ThreadPoolExecutor and ProcessPoolExecutor, subclasses of Executor, are easy-to-use modules for multitasking.

They are easy-to-use because we only need to submit a task, consisting of a function and its parameters, to the executor then the executor will run the tasks synchronously for us. For example,

from concurrent.futures import ThreadPoolExecutor

def say_something(var):
    print(var)

pool = ThreadPoolExecutor(2)
pool.submit(say_something, 'hello')
pool.submit(say_something, 'hi')

Output (the order maybe different):

hello
hi

However, the function may have multiple parameters.

def say_something(var1, var2):
    print('{}: {}'.format(var1, var2))

For such cases, we can use lambda to tackle the trick.

from concurrent.futures import ThreadPoolExecutor

def say_something (var1, var2):
    print('{}: {}'.format(var1, var2))

arr1 = ['name', 'Joldnine']
arr2 = ['email', 'heyhey@hey.com']
pool = ThreadPoolExecutor(2)
pool.submit(lambda p: say_something(*p), arr1)
pool.submit(lambda p: say_something(*p), arr2)

Output:

name: Joldnine
email: heyhey@hey.com
carlosplanchon commented 5 years ago

I have written a little piece of code based on this Issue. https://gist.github.com/carlosplanchon/e586fd9d6f0ed35cf2344bbf96c202c5