-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Making produce/consume bi-directional #4775
Labels
status:help wanted
Indicates that a maintainer wants help on an issue or pull request
Comments
Yes, I'm fine with this, since as you point out it is simply allowing a value where there was |
I remember this came up on the mailing list --- thanks for making an issue, otherwise it is easy to forget these things. |
oh, thanks! i actually forked a branch to add this, but got lost tying to understand tests... |
Needs documentation; see also #4857 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd like to suggest a change to produce and consume. I don't understand Julia's tasks in any depth, so this may not be practical, but I have used the code below and it does appear to work as expected.
The change is motivated by python's co-routines, which only have the equivalent of produce and consume (there's no yieldto), yet seem to be sufficiently general for many tasks.
The extension is pretty simple - it extends consume so that it can take an argument that is "returned" to the task being consumed, and it extends produce so that value is returned inside the task. This makes it possible to have a bi-directional "conversation" between producer and consumer. It remains asymmetric (one half calls produce, the other consume), so it's not as elegant as yieldto (which remains, of course) in some respects, but it allows the programmer to get a bit more general without needing new concepts.
Here is the implementation I have been using:
which has only a handful of lines modified from the original (adding r in produce and args in consume).
Note that this does not break existing code (as far as I can tell) because currently no return is expected from produce, and consume doesn't accept additional parameters.
Finally two examples. First, some simple Julia code that allows a cipher to be implemented as a coroutine - it receives a character of plaintext and returns the encrypted value. https://github.com/andrewcooke/Stupid.jl/blob/master/src/Cipher.jl#L112
Second, a blog post on how this is useful in Python when simulating protocols between actors http://acooke.org/cute/UsingCorou0.html
Thanks,
Andrew
The text was updated successfully, but these errors were encountered: