Skip to content

Commit

Permalink
Fixed deadlock issue in decorators when decorated function invokes `s…
Browse files Browse the repository at this point in the history
…ys.exit()`
  • Loading branch information
fernandoenzo committed Mar 5, 2024
1 parent c947dd5 commit 3c9e883
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/fernandoenzo/parallel-utils)
[![GitHub last commit](https://img.shields.io/github/last-commit/fernandoenzo/parallel-utils)](https://github.com/fernandoenzo/parallel-utils)
[![Build Status](https://img.shields.io/travis/com/fernandoenzo/parallel-utils?label=tests)](https://travis-ci.com/fernandoenzo/parallel-utils)
![Maintenance](https://img.shields.io/maintenance/yes/2022)

This library implements a class [**Monitor**](https://en.wikipedia.org/wiki/Monitor_(synchronization)), as defined by [**Per
Brinch Hansen**](https://en.wikipedia.org/wiki/Per_Brinch_Hansen) and [**C.A.R. Hoare**](https://en.wikipedia.org/wiki/Tony_Hoare),
Expand Down
27 changes: 7 additions & 20 deletions parallel_utils/process/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@ def synchronized(max_threads: int = 1):
def locked(func):
@wraps(func)
def locked_func(*args, **kw_args):
exceptions = []
s.acquire()
try:
res = func(*args, **kw_args)
except Exception as e:
exceptions.append(e)
s.release()
if len(exceptions):
raise exceptions[0]
return res
with s:
return func(*args, **kw_args)

return locked_func

return locked


def synchronized_priority(*args, **kwargs):
def synchronized_priority():
m = Monitor()

def synchronized_priority(uid: Union[str, int], order: int = 1, total: int = None):
Expand All @@ -50,17 +42,12 @@ def synchronized_priority(uid: Union[str, int], order: int = 1, total: int = Non

def locked(func):
@wraps(func)
def locked_func(*args, **kw_args):
exceptions = []
def locked_func(*args, **kwargs):
m.lock_priority_code(uid=uid, order=order, total=total)
try:
res = func(*args, **kw_args)
except Exception as e:
exceptions.append(e)
m.unlock_code(uid=uid)
if len(exceptions):
raise exceptions[0]
return res
return func(*args, **kwargs)
finally:
m.unlock_code(uid=uid)

return locked_func

Expand Down
29 changes: 8 additions & 21 deletions parallel_utils/thread/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,16 @@ def synchronized(max_threads: int = 1):

def locked(func):
@wraps(func)
def locked_func(*args, **kw_args):
exceptions = []
s.acquire()
try:
res = func(*args, **kw_args)
except Exception as e:
exceptions.append(e)
s.release()
if len(exceptions):
raise exceptions[0]
return res
def locked_func(*args, **kwargs):
with s:
return func(*args, **kwargs)

return locked_func

return locked


def synchronized_priority(*args, **kwargs):
def synchronized_priority():
m = Monitor()

def synchronized_priority(uid: Union[str, int], order: int = 1, total: int = None):
Expand All @@ -50,17 +42,12 @@ def synchronized_priority(uid: Union[str, int], order: int = 1, total: int = Non

def locked(func):
@wraps(func)
def locked_func(*args, **kw_args):
exceptions = []
def locked_func(*args, **kwargs):
m.lock_priority_code(uid=uid, order=order, total=total)
try:
res = func(*args, **kw_args)
except Exception as e:
exceptions.append(e)
m.unlock_code(uid=uid)
if len(exceptions):
raise exceptions[0]
return res
return func(*args, **kwargs)
finally:
m.unlock_code(uid=uid)

return locked_func

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name = 'parallel-utils'

# https://www.python.org/dev/peps/pep-0440/#version-scheme
version = '1.2'
version = '1.2.1'

description = 'This library implements a class Monitor, as defined by Per Brinch Hansen and C.A.R. Hoare, ' \
'for synchronization and concurrent management of threads and processes in Python. It also provides other ' \
Expand Down Expand Up @@ -52,6 +52,7 @@
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Topic :: Software Development :: Libraries :: Python Modules',
]
setuptools.setup(
Expand Down

0 comments on commit 3c9e883

Please sign in to comment.