Last active
November 17, 2020 09:14
-
-
Save yuuri/d848e95bd005811e48782e734b809d6b 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
from multiprocessing import Pool | |
import time | |
import random | |
number_list = [] | |
def sub_process(i, number): | |
rand = random.random() | |
time.sleep(rand) | |
# print('subprocess {}'.format(i)) | |
return i, number | |
def log(args): | |
i = args[0] | |
total_number = args[1] | |
# print(args) | |
number_list.append(i) | |
number = len(number_list) | |
percent = float(number) / total_number | |
print('percent is {:.2%} number is {}'.format(percent, number)) | |
def main(): | |
pool = Pool(processes=20) | |
number = 1000 | |
for i in range(1000): | |
pool.apply_async(sub_process, (i, number), callback=log) | |
pool.close() | |
pool.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment