Created
August 23, 2018 09:01
-
-
Save anjensan/a7f5039b6d1867b1fe59c5adfbec5e58 to your computer and use it in GitHub Desktop.
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 sys | |
import time | |
import random | |
import fileinput | |
import multiprocessing | |
def parse_file(f): | |
for line in f: | |
# мы прочитали строку с файла и создали message | |
yield {'line': line} | |
def process_msg(msg): | |
# очень долгая, cpu-intensive операция | |
time.sleep(0.5 + random.random()) | |
return str(msg) | |
def main(): | |
pool = multiprocessing.Pool(10) | |
for r in pool.imap(process_msg, parse_file(fileinput.input())): | |
print(">>", r) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment