Skip to content

Commit 810168c

Browse files
committed
slightly improve binding logic
1 parent 96881c3 commit 810168c

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

arq/main.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ async def shutdown(self):
8181
def _bind_concurrent(self):
8282
for attr_name in dir(self.__class__):
8383
v = getattr(self.__class__, attr_name)
84-
if isinstance(v, Concurrent):
85-
setattr(self, attr_name, v.bind(self))
84+
isinstance(v, Concurrent) and v.bind(self)
8685

8786
async def enqueue_job(self, func_name: str, *args, queue: str=None, **kwargs):
8887
"""
@@ -128,30 +127,30 @@ class Concurrent:
128127
"""
129128
__slots__ = ['_func', '_dft_queue', '_self_obj']
130129

131-
def __init__(self, func, dft_queue=None, self_obj=None):
132-
if self_obj is None:
130+
def __init__(self, *, func, dft_queue=None, self_obj=None):
131+
self._self_obj = self_obj
132+
# if we're already bound we assume func is of the correct type and skip repeat logging
133+
if not self.bound:
133134
if not inspect.iscoroutinefunction(func):
134135
raise TypeError('{} is not a coroutine function'.format(func.__qualname__))
135136

136137
main_logger.debug('registering concurrent function %s', func.__qualname__)
137138
self._func = func
138139
self._dft_queue = dft_queue
139-
self._self_obj = self_obj
140140

141-
def bind(self, obj: object) -> object:
141+
def bind(self, obj: object):
142142
"""
143-
Equivalent of binding a normal function to an object.
144-
145-
A new instance of Concurrent needs to exist for each function it's bound to.
143+
Equivalent of binding a normal function to an object. A new instance of Concurrent is created and then
144+
the reference on the parent object updated to that.
146145
147146
:param obj: object to bind the function to eg. "self" in the eyes of func.
148-
:return: instance of Concurrent, self if it's not yet bound, otherwise a new instance
149147
"""
150-
if self._self_obj is None:
151-
self._self_obj = obj
152-
return self
153-
else:
154-
return Concurrent(func=self._func, dft_queue=self._dft_queue, self_obj=obj)
148+
new_inst = Concurrent(func=self._func, dft_queue=self._dft_queue, self_obj=obj)
149+
setattr(obj, self._func.__name__, new_inst)
150+
151+
@property
152+
def bound(self):
153+
return self._self_obj is not None
155154

156155
async def __call__(self, *args, **kwargs):
157156
return await self.defer(*args, **kwargs)

0 commit comments

Comments
 (0)