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

Customize reduce message #6

Open
perrygeo opened this issue Jul 31, 2017 · 1 comment
Open

Customize reduce message #6

perrygeo opened this issue Jul 31, 2017 · 1 comment

Comments

@perrygeo
Copy link
Contributor

perrygeo commented Jul 31, 2017

The Part context manager is in charge of sending the final reduce message. But the message
structure is hardcoded; there's no way for the user of Part to specify additional data.

We need to give the user more control over the reduce message. Two options so far:

1. pass data into Part as an argument

msg = {'custom': 'hi'}
with Part(..., reduce_message=msg)
    pass

This has the advantage of being straighforward to understand and a simple implementation.
The downside is that we're forcing the user to construct the message before the context block.
It would not allow for constructing the message within the block.

My hunch is that this would be fine for most use cases and probably where we should start.

2. send data from the inner block

With some context manager tricks, it is possible for the inner block and the context manager to communicate using yield and gen.send. The example below proves it can be done - but the complexity and obscurity of this approach might be too much.

@contextmanager
def block():
    print('before')
    msg = yield  # wait for a value to be sent
    print(f'the inner block sent me {msg}')
    yield  # run remainder of inner block
    print('after')


# Have to instantiate the context manager separate from the with statement
# `with block() as context` will not work
context = block()
with context:
    print('inside')
    msg = {'custom': 42}
    # the inner block MUST send something or we get
    # a RuntimeError: generator didn't stop
    context.gen.send(msg)
    print('still inside the block')

which prints

before
inside
the inner block sent me 42
still inside the block
after

cc @dnomadb @vincentsarago and @sgillies - this context manager / generator hack is a kind of crazy but thought you might enjoy it :-)

@perrygeo
Copy link
Contributor Author

per chat, while the basic @contextmanager decorator pattern is nice, option 2 above takes this a bit too far into obscurity. We're not confident that we fully understand the underlying concepts fully enough which is a no-go.

option 1 is it for now.

@perrygeo perrygeo assigned perrygeo and unassigned perrygeo Aug 7, 2017
@perrygeo perrygeo reopened this Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant