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

Mod : Example 16-17 grouper optimization #5

Open
wants to merge 1 commit into
base: master
from

Conversation

@lbbc1117
Copy link

@lbbc1117 lbbc1117 commented May 14, 2017

The delegating generator grouper delegates averager inside a while loop.
Every time averager returns, a new but useless averager instance will be created during the following loop period.
Adding a yield statement after yield from averager() without while loop would be better.
Great great book, by the way.

@austinbravo
Copy link

@austinbravo austinbravo commented Feb 19, 2020

@ramalho i think the example should be this. currently, a new delegating generator is created for each item in data, which makes grouper's while loop unnecessary and creates the unused averager instance. grouper is also still active when the results are reported, though not sure if this matters as will be garbage collected once main() returns.

def grouper(results):
    while True:
        key = yield
        results[key] = yield from averager()

def main(data):
    results = {}
    group = grouper(results)
    next(group)
    for key, values in data.items():
        group.send(key)
        for value in values:
            group.send(value)
        group.send(None)
    group.close() # just to highlight that grouper is still an active generator

    report(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.