-
Notifications
You must be signed in to change notification settings - Fork 2
Description
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 :-)