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

bpo-30490: Allow to the Event.set method pass an exception #1824

Conversation

pfreixes
Copy link

Having the Event as the way to synchronize 1:N coroutines, the none happy path should be able to be expressed making possible call the set_exception for each future related to each waiter.

As an example the following code trying to implement a way to avoid the dogpile effect for a DNS cache. If the coro that holds the event fails, the original exception is also broadcasted to the waiters.

if key in throttle_dns_events:
    yield from throttle_dns_events[key].wait()
else:
    throttle_dns_events[key] = Event(loop=loop)
    try:
        addrs = yield from \
            resolver.resolve(host, port, family=family)
        cached_hosts.add(key, addrs)
        throttle_dns_events[key].set()
    except Exception as e:
        # any DNS exception, independently of the implementation
        # is set for the waiters to raise the same exception.
        throttle_dns_events[key].set(exc=e)
        raise
    finally:
        throttle_dns_events.pop(key)

The coro that holds the event can wake up the waiters with an exception
@mention-bot
Copy link

@pfreixes, thanks for your PR! By analyzing the history of the files in this pull request, we identified @1st1, @asvetlov and @serhiy-storchaka to be potential reviewers.

@Mariatta Mariatta added the type-feature A feature request or enhancement label May 29, 2017
@pfreixes
Copy link
Author

Any thoughts about this proposal @1st1 ?

@1st1
Copy link
Member

1st1 commented Jul 17, 2017

asyncio.Event is designed to be similar to threading.Event, and the similarity of asyncio synchronization primitives to those of the threading module is explicitly expressed in the docs. So IMO, if we want this in asyncio, we should also add this to the threading module. Would you mind looking through python-ideas archives, and if nothing is there, post this idea to discuss it on the mailing list?

@pfreixes
Copy link
Author

pfreixes commented Jul 18, 2017 via email

@pfreixes
Copy link
Author

@1st1 I'm gonna close this PR and the bug opened. I've ended up using a simple solution [1] via a wrapper that came up in that discussion [2]

[1] aio-libs/aiohttp@11afb32
[2] https://mail.python.org/pipermail/python-ideas/2017-July/046486.html

@pfreixes pfreixes closed this Jul 19, 2017
@1st1
Copy link
Member

1st1 commented Jul 19, 2017

Thanks! Yeah, it's probably easier to write a wrapper than change both threading and asyncio modules in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants