Упрощенный случай следующий (Python 3.11.8, joblib 1.4.0):
Код: Выделить всё
## lsof.py
from joblib import Parallel, delayed
import time
import sys
def f():
time.sleep(5)
return 1.0
def get_num_of_opened_files() -> int:
from subprocess import run
return int(run('lsof -u eastsun| wc -l', shell=True, capture_output=True, text=True).stdout.strip())
f0 = get_num_of_opened_files()
xs = Parallel(n_jobs=32, return_as='generator')(delayed(f)() for _ in range(32))
time.sleep(2)
f1 = get_num_of_opened_files()
print(f'Opened files: before {f0}, after {f1}, delta: {f1 - f0}', flush=True)
print(sum(xs))
Код: Выделить всё
>> python lsof.py
>> Opened files: before 388, after 3647, delta: 3259
Код: Выделить всё
## lsof2.py
from joblib import Parallel, delayed
import time
import sys
import pandas as pd
class Tasker:
def __init__(self):
self.data = pd.Series([])
def run(self):
time.sleep(10)
return 1.0
def get_num_of_opened_files() -> int:
from subprocess import run
return int(run('lsof -u eastsun| wc -l', shell=True, capture_output=True, text=True).stdout.strip())
tasker = Tasker()
f0 = get_num_of_opened_files()
xs = Parallel(n_jobs=32, return_as='generator')(delayed(tasker.run)() for _ in range(32))
time.sleep(2)
f1 = get_num_of_opened_files()
print(f'Opened files: before {f0}, after {f1}, delta: {f1 - f0}', flush=True)
print(sum(xs))
Код: Выделить всё
>> python lsof2.py
>> Opened files: before 399, after 6794, delta: 6395
Предупреждение пользователя: Рабочий остановился, когда некоторые задания были переданы
исполнителю. Это может быть вызвано слишком коротким тайм-аутом рабочего процесса или
утечкой памяти.
Как уменьшить количество открываемых файлов с помощью joblib ? Я подтвердил, что:
- Количество открытых файлов уменьшится примерно до 4, если я добавлю предпочитает='threads' в параллельном режиме . Однако мне нужна многопроцессорность, а не многопоточность.
- Изменение параметра return_as с генератора на список не дает никакого эффекта.
Около 60% открываемых файлов — это файлы .so, что меня очень смутило.
Подробнее здесь: https://stackoverflow.com/questions/784 ... many-files