Created
April 27, 2021 19:48
-
-
Save internetimagery/7861be28e67ea32fadeb7609635ee697 to your computer and use it in GitHub Desktop.
Callback experiment (async alternative)
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 ThreadPoolExecutor | |
_pool = ThreadPoolExecutor() | |
def defer(caller, *args, **kwargs): | |
def decorator(func): | |
global _pool | |
future = _pool.submit(caller, *args, **kwargs) | |
future.add_done_callback(lambda f: func(f.result())) | |
return func | |
return decorator | |
if __name__ == "__main__": | |
import time | |
def concat(a, b): | |
time.sleep(0.3) | |
return a + b | |
def main(): | |
prefix = "pre_" | |
suffix = "suff" | |
@defer(concat, prefix, suffix) | |
def _(value): | |
print("after execution", value) | |
@defer(concat, "more_", value) | |
def _(value): | |
print("more after", value) | |
print("before execution") | |
main() | |
time.sleep(0.8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment