Skip to content

Instantly share code, notes, and snippets.

@alexandru-dinu
Last active November 5, 2021 18:47
Show Gist options
  • Save alexandru-dinu/48adbd794f55c287dd865ebc7d8c3dcd to your computer and use it in GitHub Desktop.
Save alexandru-dinu/48adbd794f55c287dd865ebc7d8c3dcd to your computer and use it in GitHub Desktop.
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