Created
August 10, 2017 16:38
-
-
Save biggers/20752797e095dd992ee2d86f2a835f09 to your computer and use it in GitHub Desktop.
async Python3 code using blocking-function
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, as_completed | |
import time | |
import random | |
TPOOL_SIZE = 8 | |
def blocking(pid): | |
""" a non-deterministic "blocking" ? task | |
""" | |
sleep_for = random.randint(0, 3) * 0.001 | |
time.sleep(sleep_for) | |
return pid, sleep_for | |
def main(): | |
pool = ThreadPoolExecutor(TPOOL_SIZE) | |
futures = [] | |
for pid in range(TPOOL_SIZE): | |
futures.append(pool.submit(blocking, pid)) | |
for x in as_completed(futures): | |
print(x.result()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment