Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concurrent.futures help
#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


Messages In This Thread
concurrent.futures help - by samuelbachorik - Aug-21-2021, 08:35 PM
RE: concurrent.futures help - by Yoriz - Aug-21-2021, 08:47 PM
RE: concurrent.futures help - by bowlofred - Aug-22-2021, 07:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 2,015 May-03-2023, 08:22 AM
Last Post: billykid999
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,974 Nov-12-2021, 09:55 PM
Last Post: snippsat
  Problem with concurrent.futures. thunderspeed 3 2,144 Sep-01-2021, 05:21 PM
Last Post: snippsat
  Trying to understand concurrent.futures.ThreadPoolExecutor Laxminarsaiah 0 1,648 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,990 Dec-23-2017, 10:24 PM
Last Post: saavedra29
  Need my program to run concurrent operations cygnus_X1 1 3,187 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