3 ms·
The code needs some minor changes (imports, missing parameter and indent) to make it runnable. from concurrent.futures import ProcessPoolExecutor import st
by BSVogler 7y ago
The code needs some minor changes (imports, missing parameter and indent) to make it runnable.
from concurrent.futures import ProcessPoolExecutor
import string
import random
import time
def hello(output) -> int:
seconds = random.randint(0, 5)
print(f'Hi {output} {seconds}s')
time.sleep(seconds)
print(f'Bye {seconds}s')
return seconds
# Don't forget this for processes, or you'll get in trouble
if __name__ == "__main__":
inputs = list(string.printable)
results = []
# You can sub out ProcessPool with ThreadPool.
with ProcessPoolExecutor() as executor:
results += executor.map(hello, inputs)
[print(s) for s in results]
EDIT: I also struggle with proper code indentation on HN.