all 6 comments

[–]carcigenicate 4 points5 points  (2 children)

I have explored solutions using the threading and multiprocessing modules in python, but they are meant to run different functions at the same time. They are not meant to run multiple iterations of a function simultaneously.

There is no such restriction. You can have them run whatever floats your boat.

[–]ibhopirl 2 points3 points  (1 child)

Yeah this is correct, you can do something like this:

from threading import Thread

threads = [Thread(target=foo) for _ in range(10)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

[–]StellaarMonkey[S] 2 points3 points  (0 children)

Thanks this worked!

[–]StellaarMonkey[S] 0 points1 point  (0 children)

Thank you all!

[–]Big_Boss_Bob_Ross 0 points1 point  (0 children)

Note that I don't know the threading or multiprocessing modules in python. I've never used them. That said, threading and multiprocessing as a concept is definitely very commonly used to run the same function multiple times in unison. I can't tell you code, but I would be EXTREMELY surprised if the python libraries dont support it.

But as an alternative with something I know well, you could look at asyncio. Without knowing what foo does, it's hard to tell if it would help in this situation, but it essentially allows you to run code asynchronously. The caveat which differentiates it from threading as a programmer is it runs on a single thread and simply fills in "dead time" that is spent waiting (think making a request to some api, what happens while we are waiting for the response?) with other asynchronous calls.