Run functions in parallel safely with typesafe parmap!.
Project description
Typesafe parmap
pip install typesafe-parmap
Run functions in parallel safely with your type checkers
Features
Easy run different functions in parallel
from typesafe_parmap import par_map_2
import time
from concurrent.futures import ThreadPoolExecutor
tp = ThreadPoolExecutor(5)
def long_running_int(param: int) -> int:
time.sleep(5) # long IO task here
return 123
def long_running_str(param: str) -> str:
time.sleep(5) # long IO task here
return "hello world"
int_result, str_result = par_map_2(
lambda: long_running_int(5),
lambda: long_running_str("test"),
executor=tp)
assert int_result == 123, str_result == "hello world" # should finish in around 5 seconds
Function return types are inferred correctly by mypy / pycharm
reveal_type(int_result) # mypy infers int
reveal_type(str_result) # mypy infers str
Accidentally unpacked too many / little values? Type inference checks that for you!
one, two, three, four = par_map_3(
lambda: long_running_int(5), lambda: long_running_str("test"), lambda: "something", executor=tp
) # Error: Need more than 3 values to unapck, (4 expected)
Got more than a few functions to run? We got you covered...
from typesafe_parmap import par_map_4 # ... all the way to par_map_22!
Want to change the number of functions to run in parallel? Hate having to import a different one each time? Use par_map_n!
a = par_map_2(lambda: long_running_int(5), lambda: long_running_str("test"), executor=executor)
b = par_map_n(lambda: long_running_int(5), lambda: long_running_str("test"), executor=executor)
assert a == b
c = par_map_3(lambda: long_running_int(5), lambda: long_running_str("test"), lambda: long_running_str("test"), executor=executor)
d = par_map_n(lambda: long_running_int(5), lambda: long_running_str("test"), lambda: long_running_str("test"), executor=executor)
assert c == d
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
typesafe_parmap-1.0.3.tar.gz
(7.1 kB
view hashes)
Built Distribution
Close
Hashes for typesafe_parmap-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a66c26c6b0dfb32188237b0cf6bfca1a16255d03204dd29e34c40cdcd37324e0 |
|
MD5 | 26e9ad7c8927ceae43b30d2f250e9d5d |
|
BLAKE2b-256 | 0a25211e843e6ec33399948365a96515fe68d9edbc5606da927e5753fed8f01d |