Created
August 11, 2018 20:05
-
-
Save santiagobasulto/2bc51e7b0b0c7c2a74fd6d5748778dd7 to your computer and use it in GitHub Desktop.
Pickle limitations for class based decorators.
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 concurrent.futures import ProcessPoolExecutor | |
class CheckOnlyIntegers: | |
def __init__(self, fn): | |
self.fn = fn | |
def __call__(self, *args): | |
if not all([type(arg) == int for arg in args]): | |
raise ValueError("Invalid param is not an integer") | |
return self.fn(*args) | |
@CheckOnlyIntegers # this will change in the fixed version | |
def add(x, y): | |
return x + y | |
with ProcessPoolExecutor(max_workers=3) as ex: | |
futures = [ | |
ex.submit(add, *(2, 3)), | |
ex.submit(add, *(9, 8)), | |
] | |
for future in futures: | |
print(future.result(timeout=3)) |
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 concurrent.futures import ProcessPoolExecutor | |
class CheckOnlyIntegers: | |
def __init__(self, fn): | |
self.fn = fn | |
def __call__(self, *args): | |
if not all([type(arg) == int for arg in args]): | |
raise ValueError("Invalid param is not an integer") | |
return self.fn(*args) | |
def add(x, y): | |
return x + y | |
alternate_add = CheckOnlyIntegers(add) # Used instead of decorator | |
with ProcessPoolExecutor(max_workers=3) as ex: | |
futures = [ | |
ex.submit(alternate_add, *(2, 3)), | |
ex.submit(alternate_add, *(9, 8)), | |
] | |
for future in futures: | |
print(future.result(timeout=3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment