updated chapter 4 and appendix-A files
This commit is contained in:
38
appendix-A/sha_futures.py
Normal file
38
appendix-A/sha_futures.py
Normal file
@@ -0,0 +1,38 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user