<@ULVA73B9P> please demo how to run many instances...
# ask-marvin
n
@Marvin please demo how to run many instances of a sync function concurrently with a thread pool executor in normal python be concise and optimize for easy transition from previously using this pattern
Copy code
for i in range(N):
  call_sync_thing(...)
m
Certainly. You can use the
concurrent.futures
module to achieve this. Here's a concise example demonstrating how to run many instances of a synchronous function concurrently using a `ThreadPoolExecutor`:
Copy code
python
import concurrent.futures

# Define your synchronous function
def call_sync_thing(arg):
    # The actual implementation of your function
    ...

# Number of times you want to call the function
N = 10

# Arguments for the function (if any)
args_list = [arg1, arg2, ...]  # Adjust as per your requirement

# Create a ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor() as executor:
    # Use a list comprehension to submit all tasks
    futures = [executor.submit(call_sync_thing, arg) for arg in args_list[:N]]
    
    # Optionally, you can gather results if needed
    results = [future.result() for future in concurrent.futures.as_completed(futures)]

# Now `results` contains the return values from `call_sync_thing`
### Explanation: 1. Import
concurrent.futures
: This module supports launching parallel tasks. 2. Define
call_sync_thing
: Your synchronous function. 3. Set
N
and
args_list
: The number of instances and the arguments for each call. 4. Create a
ThreadPoolExecutor
: Opens a pool of threads. 5. Submit tasks using a list comprehension: Uses
executor.submit
to submit tasks to the executor. 6. Gather results: Utilizes
concurrent.futures.as_completed
to get the results as they complete. This should allow for an easy transition from a simple loop to a concurrent execution pattern.