Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concurrent.futures help
#1
Hi i want to ask you what is the difference betwen map() and submit()

Two codes examples

        with concurrent.futures.ProcessPoolExecutor() as executor:

            results = [None] * 50
            for x in range(50):
                results[x] = executor.map(function)
        with concurrent.futures.ProcessPoolExecutor() as executor:

            results = [None] * 50
            for x in range(50):
                results[x] = executor.submit(lfunction)
Reply
#2
https://docs.python.org/3/library/concur...tor.submit Wrote:submit(fn, /, *args, **kwargs)
Schedules the callable, fn, to be executed as fn(*args **kwargs) and returns a Future object representing the execution of the callable

https://docs.python.org/3/library/concur...ecutor.map Wrote:map(func, *iterables, timeout=None, chunksize=1)
Similar to map(func, *iterables) except:
  • The iterables are collected immediately rather than lazily;
  • func is executed asynchronously and several calls to func may be made concurrently.

I used ThreadPoolExecutor as ProcessPoolExecutor never works for me
from concurrent.futures import ThreadPoolExecutor


def func(value):
    return str(value)


with ThreadPoolExecutor() as executor:

    results = []
    for number in range(50):
        results.append(executor.submit(func, number))


print([result.result() for result in results])

with ThreadPoolExecutor() as executor:
    results = list(executor.map(func, range(50)))

print(results)
Output:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49'] ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49']
Reply
#3
(Aug-21-2021, 08:35 PM)samuelbachorik Wrote: Hi i want to ask you what is the difference betwen map() and submit()

map() can be very convenient because you get the actual result from it rather than have to process a collection of future objects. Often this is sufficient.

What you lose from it is that the results must be returned in order. Most of the time that's fine, but there are some situations where you'd rather have the flexibility of being able to process in completion order instead. Also, any exceptions thrown by any child will stop the entire map(). If you want to process the data from the ones without exceptions, you need to work with the future objects yourself.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,811 May-03-2023, 08:22 AM
Last Post: billykid999
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,874 Nov-12-2021, 09:55 PM
Last Post: snippsat
  Problem with concurrent.futures. thunderspeed 3 2,043 Sep-01-2021, 05:21 PM
Last Post: snippsat
  Trying to understand concurrent.futures.ThreadPoolExecutor Laxminarsaiah 0 1,615 Dec-18-2019, 12:43 PM
Last Post: Laxminarsaiah
  asyncio: run_until_complete() returns when the first of gathered Futures is set saavedra29 0 3,941 Dec-23-2017, 10:24 PM
Last Post: saavedra29
  Need my program to run concurrent operations cygnus_X1 1 3,124 Jul-21-2017, 10:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020