Home  >  Article  >  Backend Development  >  How to pass single parameter and multiple parameters to python thread pool ThreadPoolExecutor

How to pass single parameter and multiple parameters to python thread pool ThreadPoolExecutor

王林
王林forward
2023-04-19 19:22:042425browse

    Python thread pool ThreadPoolExecutor, passing single parameter and multiple parameters

    This is the thread pool passing a single parameter

    from concurrent.futures import ThreadPoolExecutor,as_completed
    def test(a):
            print(a)
    
    qq = {"a":"1","b":"2","c":"3"}
    with ThreadPoolExecutor() as pool:
        for j ,k in qq.items():
            res = pool.submit(test,j)
            kk = res.result()

    The following is The main method of passing multiple parameters is pool.submit(lambda cxp:test(*cxp),(j,k))

    This line of code needs to be disassembled and viewed

    The first is the anonymous function: lambda cxp:test(*cxp) This is the first step

    This means: pass the cxp parameter to test

    The second step is submit(lambda cxp:test(cxp),(j,k))

    sumbit method requires passing two parameters, the first one is function, the second one is the parameter of this function

    The anonymous function just now is the first parameter, and then (j,k) is the second parameter. This parameter is to be passed to the function, so (j,k ) gives cxp

    python thread pool to pass in multiple parameters ThreadPoolExecutor.submit multi-parameter support

    from concurrent.futures import ThreadPoolExecutor,as_completed
    def test(a,b):
            print(a,b)
    
    qq = {"a":"1","b":"2","c":"3"}
    with ThreadPoolExecutor() as pool:
        for j ,k in qq.items():
            res = pool.submit(lambda cxp:test(*cxp),(j ,k))
            last= res.result())

    The above is the detailed content of How to pass single parameter and multiple parameters to python thread pool ThreadPoolExecutor. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete