Skip to content

Commit

Permalink
issue #147: expose channel lock timeout parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Cafasso <[email protected]>
  • Loading branch information
noxdafox committed Jan 25, 2025
1 parent f3b9d23 commit 036da68
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pebble/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ class Consts:
term_timeout: float = 3
"""On UNIX once a SIGTERM signal is issued to a process,
the amount of seconds to wait before issuing a SIGKILL signal."""
channel_lock_timeout: float = 60
"""The process pool relies on a pipe protected by a lock.
The timeout when attempting to acquire the lock."""


try:
Expand Down
19 changes: 12 additions & 7 deletions pebble/pool/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import select
import multiprocessing

from typing import Any, Callable
from contextlib import contextmanager
from typing import Any, Callable, Tuple

from pebble.common import CONSTS


class ChannelError(OSError):
Expand Down Expand Up @@ -150,11 +152,15 @@ def __exit__(self, *_):

def _make_acquire_method(self) -> Callable:
def unix_acquire() -> bool:
return (self.reader_mutex.acquire(timeout=LOCK_TIMEOUT) and
self.writer_mutex.acquire(timeout=LOCK_TIMEOUT))
return (
self.reader_mutex.acquire(timeout=CONSTS.channel_lock_timeout)
and
self.writer_mutex.acquire(timeout=CONSTS.channel_lock_timeout)
)

def windows_acquire() -> bool:
return self.reader_mutex.acquire(timeout=LOCK_TIMEOUT)
return self.reader_mutex.acquire(
timeout=CONSTS.channel_lock_timeout)

return unix_acquire if os.name != 'nt' else windows_acquire

Expand All @@ -171,7 +177,7 @@ def windows_release():
@property
@contextmanager
def reader(self):
if self.reader_mutex.acquire(timeout=LOCK_TIMEOUT):
if self.reader_mutex.acquire(timeout=CONSTS.channel_lock_timeout):
try:
yield self
finally:
Expand All @@ -182,7 +188,7 @@ def reader(self):
@property
@contextmanager
def writer(self):
if self.writer_mutex.acquire(timeout=LOCK_TIMEOUT):
if self.writer_mutex.acquire(timeout=CONSTS.channel_lock_timeout):
try:
yield self
finally:
Expand All @@ -192,4 +198,3 @@ def writer(self):


MILLISECONDS = 1000
LOCK_TIMEOUT = 60

0 comments on commit 036da68

Please sign in to comment.