Last active
July 11, 2023 14:06
-
-
Save heymarco/0dea6fc8121b8639d8702687a97a8c54 to your computer and use it in GitHub Desktop.
Asynchronous execution of python function with ordered result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
from time import sleep | |
def _wait(i): | |
sleep(i) | |
return i | |
def run_async(function, args_list, njobs, sleep_time_s = 0.01): | |
pool = Pool(njobs) | |
results = [pool.apply_async(function, args=args) for args in args_list] | |
while not all(future.ready() for future in results): | |
sleep(sleep_time_s) | |
results = [result.get() for result in results] | |
pool.close() | |
return results | |
if __name__ == '__main__': | |
njobs = 3 | |
delay = [[i] for i in range(3)] | |
result = run_async(_wait, delay, njobs) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment