Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass multiple arguments to concurrent.futures.ThreadPoolExecutor()

I’m trying to run a function with 2 arguments using concurrent.futures.ThreadPoolExecutor()

Function:

def test(var1, var2):
    return var1 + var2

How do I pass x and y as the two arguments / values for this function?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x = 'text1'
y = 'text2'
process = concurrent.futures.ThreadPoolExecutor().submit(test, PASS_TWO_ARGUMENTS_HERE)
z = process.results()

I found various answers, but they all mentioned complex cases and solutions; can someone provide a simple 1-line solution for this without changing the function itself?

>Solution :

You can use lambda function for your purposes:

from concurrent.futures import ThreadPoolExecutor

def test(var1, var2):
    return var1 + var2

params = ['text1', 'text2']
process = concurrent.futures.ThreadPoolExecutor().submit(lambda p: test(*p), params)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading