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

Temporarily disable a signal in a block #64

Closed
gergelypolonkai opened this issue Mar 12, 2021 · 2 comments
Closed

Temporarily disable a signal in a block #64

gergelypolonkai opened this issue Mar 12, 2021 · 2 comments

Comments

@gergelypolonkai
Copy link

I have an application that generates reports from a bunch of sources. When a report is generated, a Blinker signal report_created is called, and one of the receivers sends an email to subscribers of that report type.

It sometimes happens, however, that we want to (re/)generate a large amount of reports. In such cases we don’t want email sending to happen. However, if an actual report gets generated meanwhile by the normal means, i want the receiver to be called, so i can’t just disconnect that receiver.

What i could imagine is something like this:

with report_created.disable():
    generate_a_lot_of_reports()

As far as i understand, this is not possible in the current Blinker version, but please correct me if i’m wrong. Also, if this use case seems valid (ie. not a unicorn case, when only my project needs it) i’m willing to dig deeper and submit a PR for this.

@gergelypolonkai
Copy link
Author

As a workaround, this seems to work, there might be unexpected side effects, e.g. in a threaded environment:

from contextlib import contextmanager

def disable_signal(signal):
    original_receivers = signal.receivers
    signal.receivers = {}

    try:
        yield
    finally:
        signal.receivers = original_receivers

@davidism davidism linked a pull request Jul 19, 2022 that will close this issue
@Secrus
Copy link
Member

Secrus commented Jan 24, 2023

Done in #84

@Secrus Secrus closed this as completed Jan 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants