Skip to content

Commit

Permalink
Locker "with" support
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Aug 5, 2019
1 parent 13f9b5f commit 09b74ca
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion atasker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, http://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.2.9"
__version__ = "0.2.10"

from atasker.supervisor import TaskSupervisor
from atasker.supervisor import TASK_LOW
Expand Down
2 changes: 1 addition & 1 deletion atasker/f.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, http://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.2.9"
__version__ = "0.2.10"

import traceback
import threading
Expand Down
2 changes: 1 addition & 1 deletion atasker/supervisor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, http://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.2.9"
__version__ = "0.2.10"

import threading
import multiprocessing
Expand Down
21 changes: 17 additions & 4 deletions atasker/threads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, http://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.2.9"
__version__ = "0.2.10"

import threading
import time
Expand Down Expand Up @@ -76,10 +76,10 @@ class Locker:
relative: True for RLock (default), False for Lock
"""

def __init__(self, mod='', timeout=5, relative=True):
def __init__(self, mod='main', timeout=5, relative=True):
self.lock = threading.RLock() if relative else threading.Lock()
self.logger = logging.getLogger('atasker/locker')
self.mod = '' if not mod else mod + '/'
self.mod = mod
self.relative = relative
self.timeout = timeout

Expand All @@ -88,7 +88,7 @@ def __call__(self, f):
@wraps(f)
def do(*args, **kwargs):
if not self.lock.acquire(timeout=self.timeout):
self.logger.critical('{}{} locking broken'.format(
self.logger.critical('{}/{} locking broken'.format(
self.mod, f.__name__))
self.critical()
return None
Expand All @@ -99,6 +99,19 @@ def do(*args, **kwargs):

return do

def __enter__(self):
"""
Raises:
TimeoutError: if lock not acquired
"""
if not self.lock.acquire(timeout=self.timeout):
self.logger.critical('{} locking broken'.format(self.mod))
self.critical()
raise TimeoutError

def __exit__(self, *args, **kwargs):
self.lock.release()

def critical(self):
"""
Override this
Expand Down
2 changes: 1 addition & 1 deletion atasker/workers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, http://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.2.9"
__version__ = "0.2.10"

import threading
import logging
Expand Down
12 changes: 9 additions & 3 deletions doc/locker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ Locker helper/decorator
import os, signal
os.kill(os.getpid(), signal.SIGKILL)
with_lock1 = Locker(mod='main', timeout=5)
with_lock1.critical = critical_exception
lock1 = Locker(mod='main', timeout=5)
lock1.critical = critical_exception
# use as decorator
@with_lock1
def test():
# thread-safe access to resources locked with with_lock1
# thread-safe access to resources locked with lock1
# with
with lock1:
# thread-safe access to resources locked with lock1
Supports methods:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.2.9"
__version__ = "0.2.10"

import setuptools

Expand Down

0 comments on commit 09b74ca

Please sign in to comment.