Last active
November 5, 2021 18:47
-
-
Save alexandru-dinu/48adbd794f55c287dd865ebc7d8c3dcd to your computer and use it in GitHub Desktop.
Relevant issue: https://stackoverflow.com/a/65152341
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 logging | |
import multiprocessing as mp | |
def work(x): | |
ys = [] | |
if x % 2 == 0: | |
for i in range(10): | |
y = bytearray(1024*1024*1024 * 1) | |
ys.append(y) | |
time.sleep(i) | |
return len(ys) | |
return 0 | |
if __name__ == '__main__': | |
logger = mp.get_logger() | |
logger.setLevel(logging.DEBUG) | |
formatter = logging.Formatter( | |
fmt='[%(process)d] %(message)s', | |
datefmt='%y-%m-%d %H:%M:%S') | |
handler = logging.StreamHandler(sys.stdout) | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
N = 16 | |
out = [] | |
with mp.Pool(processes=8, maxtasksperchild=1) as pool: | |
for y in pool.imap_unordered(work, range(N)): | |
out.append(y) | |
logger.debug('%2d/%2d', len(out), N) | |
print('done') | |
print(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment