Created
August 11, 2020 02:31
-
-
Save ciela/75f4cb6a9091eb91d0297f279678decc to your computer and use it in GitHub Desktop.
Trial of ProcessPoolExecutor
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
import time | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
import random | |
import jsonlines as jl | |
class MyExecutor: | |
def write(self, text: str): | |
time.sleep(random.randint(0, 2)) | |
return {"now": time.time(), "text": text} | |
def run(self): | |
print("start") | |
with ProcessPoolExecutor(max_workers=4) as executor, jl.open( | |
"executor_test.jsonl", "w" | |
) as writer: | |
futures = [executor.submit(self.write, f"test{i}") for i in range(10)] | |
print("dispatched") | |
for future in as_completed(futures): | |
writer.write(future.result()) | |
print("end") | |
if __name__ == "__main__": | |
MyExecutor().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment