- add async callback to close pool after 10 minutes of inactivity

This commit is contained in:
Wim Pomp
2024-11-05 15:05:10 +01:00
parent 46bd419d3d
commit 7291468fb7
2 changed files with 10 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import asyncio
import multiprocessing import multiprocessing
from collections import UserDict from collections import UserDict
from contextlib import redirect_stderr, redirect_stdout from contextlib import redirect_stderr, redirect_stdout
@@ -298,23 +299,26 @@ class PoolSingleton:
new.is_alive = True new.is_alive = True
new.handle = 0 new.handle = 0
new.pools = {} new.pools = {}
new.time_out = None
cls.instance = new cls.instance = new
return cls.instance return cls.instance
def __init__(self, n_processes: int = None, parpool: Parpool = None) -> None: # noqa def __init__(self, n_processes: int = None, parpool: Parpool = None) -> None: # noqa
if parpool is not None: if parpool is not None:
self.pools[parpool.id] = parpool self.pools[parpool.id] = parpool
if self.time_out is not None:
self.time_out.cancel()
self.time_out = None
def __getstate__(self) -> NoReturn: def __getstate__(self) -> NoReturn:
raise RuntimeError(f'Cannot pickle {self.__class__.__name__} object.') raise RuntimeError(f'Cannot pickle {self.__class__.__name__} object.')
# def __del__(self):
# self.close()
def remove_pool(self, pool_id: int) -> None: def remove_pool(self, pool_id: int) -> None:
self.shared_memory.remove_pool(pool_id) self.shared_memory.remove_pool(pool_id)
if pool_id in self.pools: if pool_id in self.pools:
self.pools.pop(pool_id) self.pools.pop(pool_id)
if len(self.pools) == 0:
self.time_out = asyncio.get_event_loop().call_later(600, self.close) # noqa
def error(self, error: Exception) -> NoReturn: def error(self, error: Exception) -> NoReturn:
self.close() self.close()
@@ -365,6 +369,8 @@ class PoolSingleton:
if cls.instance is not None: if cls.instance is not None:
instance = cls.instance instance = cls.instance
cls.instance = None cls.instance = None
if instance.time_out is not None:
instance.time_out.cancel()
def empty_queue(queue): def empty_queue(queue):
try: try:

View File

@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "parfor" name = "parfor"
version = "2024.11.0" version = "2024.11.1"
description = "A package to mimic the use of parfor as done in Matlab." description = "A package to mimic the use of parfor as done in Matlab."
authors = ["Wim Pomp <wimpomp@gmail.com>"] authors = ["Wim Pomp <wimpomp@gmail.com>"]
license = "GPLv3" license = "GPLv3"