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

王林
Release: 2023-04-19 19:22:04
forward
2494 people have browsed it

    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()
    Copy after login

    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())
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!