Skip to content

Commit

Permalink
fixed #22
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmaslanka committed Apr 4, 2024
1 parent 2d69f81 commit 662ec89
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ have been made so far, between releases.

# v1.3.1

* coolamqp.objects.Callable made threadsafe (fixes #22)
2 changes: 1 addition & 1 deletion coolamqp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.3.1a1'
__version__ = '1.3.1a2'
18 changes: 10 additions & 8 deletions coolamqp/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Core objects used in CoolAMQP
"""
import logging
import threading
import typing as tp
import uuid

Expand Down Expand Up @@ -33,23 +34,24 @@ class Callable(object):
"""
Add a bunch of callables to one list, and just invoke'm.
INTERNAL USE ONLY
#todo not thread safe
"""
__slots__ = ('callables', 'oneshots')
__slots__ = ('callables', 'oneshots', 'lock')

def __init__(self, oneshots=False):
""":param oneshots: if True, callables will be called and discarded"""
self.callables = []
self.lock = threading.Lock()
self.oneshots = oneshots

def add(self, callable):
self.callables.append(callable)
def add(self, clbl):
self.callables.append(clbl)

def __call__(self, *args, **kwargs):
for callable in self.callables:
callable(*args, **kwargs)
if self.oneshots:
self.callables = []
with self.lock:
for clbl in self.callables:
clbl(*args, **kwargs)
if self.oneshots:
self.callables = []


class Message(object):
Expand Down

0 comments on commit 662ec89

Please sign in to comment.