How to run functions in parallel?

后端 未结 6 1095
我在风中等你
我在风中等你 2020-11-22 10:32

I researched first and couldn\'t find an answer to my question. I am trying to run multiple functions in parallel in Python.

I have something like this:



        
6条回答
  •  没有蜡笔的小新
    2020-11-22 10:56

    Seems like you have a single function that you need to call on two different parameters. This can be elegantly done using a combination of concurrent.futures and map with Python 3.2+

    import time
    from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
    
    def sleep_secs(seconds):
      time.sleep(seconds)
      print(f'{seconds} has been processed')
    
    secs_list = [2,4, 6, 8, 10, 12]
    

    Now, if your operation is IO bound, then you can use the ThreadPoolExecutor as such:

    with ThreadPoolExecutor() as executor:
      results = executor.map(sleep_secs, secs_list)
    

    Note how map is used here to map your function to the list of arguments.

    Now, If your function is CPU bound, then you can use ProcessPoolExecutor

    with ProcessPoolExecutor() as executor:
      results = executor.map(sleep_secs, secs_list)
    

    If you are not sure, you can simply try both and see which one gives you better results.

    Finally, if you are looking to print out your results, you can simply do this:

    with ThreadPoolExecutor() as executor:
      results = executor.map(sleep_secs, secs_list)
      for result in results:
        print(result)
    

提交回复
热议问题