39 lines
901 B
Python
39 lines
901 B
Python
import sys
|
|
import time
|
|
import hashlib
|
|
from concurrent import futures
|
|
from random import randrange
|
|
|
|
JOBS = 12
|
|
SIZE = 2**20
|
|
STATUS = '{} workers, elapsed time: {:.2f}s'
|
|
|
|
|
|
def sha(size):
|
|
data = bytearray(randrange(256) for i in range(size))
|
|
algo = hashlib.new('sha256')
|
|
algo.update(data)
|
|
return algo.hexdigest()
|
|
|
|
|
|
def main(workers=None):
|
|
if workers:
|
|
workers = int(workers)
|
|
t0 = time.time()
|
|
|
|
with futures.ProcessPoolExecutor(workers) as executor:
|
|
actual_workers = executor._max_workers
|
|
to_do = (executor.submit(sha, SIZE) for i in range(JOBS))
|
|
for future in futures.as_completed(to_do):
|
|
res = future.result()
|
|
print(res)
|
|
|
|
print(STATUS.format(actual_workers, time.time() - t0))
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) == 2:
|
|
workers = int(sys.argv[1])
|
|
else:
|
|
workers = None
|
|
main(workers)
|