Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure proper order of signal emission #281

Merged
merged 8 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/psygnal/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def __init__(
self._is_blocked: bool = False
self._is_paused: bool = False
self._lock = threading.RLock()
self._emit_queue: list[tuple] = []

@staticmethod
def _instance_ref(instance: Any) -> Callable[[], Any]:
Expand Down Expand Up @@ -944,22 +945,35 @@ def __call__(
check_types=check_types,
)

def _run_emit_loop_inner(self) -> None:
try:
with Signal._emitting(self):
q_len = len(self._emit_queue)
for i in range(q_len):
args = self._emit_queue[i]
for caller in self._slots:
caller.cb(args)
self._emit_queue = self._emit_queue[q_len:]
if len(self._emit_queue) > 0:
self._run_emit_loop_inner()
except RecursionError as e:
raise RecursionError(
f"RecursionError in {caller.slot_repr()} when "
f"emitting signal {self.name!r} with args {args}"
) from e
except Exception as e:
raise EmitLoopError(cb=caller, args=args, exc=e, signal=self) from e

def _run_emit_loop(self, args: tuple[Any, ...]) -> None:
# allow receiver to query sender with Signal.current_emitter()
with self._lock:
with Signal._emitting(self):
for caller in self._slots:
try:
caller.cb(args)
except RecursionError as e:
raise RecursionError(
f"RecursionError in {caller.slot_repr()} when "
f"emitting signal {self.name!r} with args {args}"
) from e
except Exception as e:
raise EmitLoopError(
cb=caller, args=args, exc=e, signal=self
) from e
self._emit_queue.append(args)
if len(self._emit_queue) > 1:
return None
try:
self._run_emit_loop_inner()
finally:
self._emit_queue.clear()

return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! Thanks.

I'm not worried about the very small benchmark decreases on emit to setattr and setitem for large numbers of callbacks.

Thanks for working on this


Expand Down Expand Up @@ -1108,6 +1122,7 @@ def __getstate__(self) -> dict:
"_args_queue",
"_check_nargs_on_connect",
"_check_types_on_connect",
"_emit_queue",
)
dd = {slot: getattr(self, slot) for slot in attrs}
dd["_instance"] = self._instance()
Expand Down
26 changes: 26 additions & 0 deletions tests/containers/test_evented_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,29 @@ def test_copy_no_sync():
s2 = copy(s1)
s1.add(4)
assert len(s2) == 3


def test_set_emission_order():
s = EventedSet()

def callback1():
if 1 not in s:
s.add(1)

def callback2():
if 5 not in s:
s.update(range(5, 10))

s.events.items_changed.connect(callback1)
s.events.items_changed.connect(callback2)
mock = Mock()
s.events.items_changed.connect(mock)

s.add(11)
mock.assert_has_calls(
[
call((11,), ()),
call((1,), ()),
call((5, 6, 7, 8, 9), ()),
]
)
50 changes: 50 additions & 0 deletions tests/test_psygnal.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,53 @@ def callback() -> None:

with pytest.raises(RecursionError):
s.emit()


def test_signal_order():
"""Test that signals are emitted in the order they were connected."""
emitter = Emitter()
mock1 = Mock()
mock2 = Mock()

def callback(x):
if x != 10:
emitter.one_int.emit(10)

emitter.one_int.connect(mock1)
emitter.one_int.connect(callback)
emitter.one_int.connect(mock2)
emitter.one_int.emit(1)

mock1.assert_has_calls([call(1), call(10)])
mock2.assert_has_calls([call(1), call(10)])


def test_signal_order_suspend():
"""Test that signals are emitted in the order they were connected."""
emitter = Emitter()
mock1 = Mock()
mock2 = Mock()

def callback(x):
if x < 10:
emitter.one_int.emit(10)

def callback2(x):
if x == 10:
emitter.one_int.emit(11)

def callback3(x):
if x == 10:
with emitter.one_int.paused(reducer=lambda a, b: (a[0] + b[0],)):
for i in range(12, 15):
emitter.one_int.emit(i)

emitter.one_int.connect(mock1)
emitter.one_int.connect(callback)
emitter.one_int.connect(callback2)
emitter.one_int.connect(callback3)
emitter.one_int.connect(mock2)
emitter.one_int.emit(1)

mock1.assert_has_calls([call(1), call(10), call(11), call(39)])
mock2.assert_has_calls([call(1), call(10), call(11), call(39)])
Loading