Skip to content

Instantly share code, notes, and snippets.

@heymarco
Last active July 11, 2023 14:06
Show Gist options
  • Save heymarco/0dea6fc8121b8639d8702687a97a8c54 to your computer and use it in GitHub Desktop.
Save heymarco/0dea6fc8121b8639d8702687a97a8c54 to your computer and use it in GitHub Desktop.
Asynchronous execution of python function with ordered result
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