Last active
April 19, 2021 16:54
-
-
Save Grohden/fb5413f4b492869f985e831f91a5ff66 to your computer and use it in GitHub Desktop.
Promise.all like function for python running in threads
This file contains hidden or 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
import concurrent.futures | |
from time import sleep | |
def expensive_computation(number): | |
sleep(10) | |
return number | |
class Runnable: | |
def __init__(self, original_target, *args, **kwargs): | |
self.original_target = original_target | |
self.args = args | |
self.kwargs = kwargs | |
def run(self): | |
self.original_target(*self.args, **self.kwargs) | |
def run_parallel(*fns: Runnable): | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
futures = [ | |
executor.submit(fn.original_target, *fn.args, **fn.kwargs) for fn in fns | |
] | |
return [future.result() for future in futures] | |
# PARALLEL | |
a, b, c = run_parallel( | |
Runnable(expensive_computation, 1), | |
Runnable(expensive_computation, 2), | |
Runnable(expensive_computation, 3), | |
) | |
print(a) | |
print(b) | |
print(c) | |
print("\n") | |
# SEQUENTIAL | |
for number in [1,2,3]: | |
print(expensive_computation(number)) | |
# Do keep in mind the python GIL limitation. threading will only be good if your computation | |
# happens outside of python (or if it escapes python ASAP, like an http request does) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment