On Sun, Aug 11, 2019 at 8:37 AM Stephen R. van den Berg srb@cuci.nl wrote:
Chris Angelico wrote:
Pike now (8.1) has a decent promise/future subsystem. It's always had great handling of multiple asynchronous operations (GUI, socket, time delay, etc) with the convenience of just returning -1 from main. Interested in people's opinions on whether it would be of value to introduce generators and (built on them) asynchronous functions.
void show_channel_info(string name) { int id = yield get_channel_id(name); mapping data = yield query_channel(id); write("Status: %s\n", data->status); mapping stream = yield get_stream_info(id); if (stream) write("%s is online, %s\n", name, stream->uptime); else write("%s is offline.\n", name);
This is WAY more readable than the equivalent with a bunch of lambda functions to capture the intermediate values.
Some "random" thoughts on this:
- I've considered the readability aspect, and I fully agree that having async functions with await/yield functionality is a boost to readability; and thus it is desirable to have the ability to use it in Pike.
- I have not thought through your generator description; a cursory reading seems to indicate that has some desirable features.
- I see that in your example you use a "yield" keyword. Is there a reason to not use the JavaScript compatible "await" instead?
No particular reason. In describing generators, I used the generator keyword from both Python and JavaScript, which is "yield"; async functions in both languages use "await". For this extremely high level summary of the proposal, either keyword makes sense.
I'm not sure whether we'll actually need both forms; it might be possible to build async functions directly on generators, without the notion of "generator delegation" (in Python, that's spelled "yield from").
The essential features would be:
1) Resumable functions, or a way to encapsulate a stack frame to be resumed later. Has a lot of consequences eg tracebacks.
2) A way to tag a function as "this will return a Concurrent.Future that resolves with its final return value, and it can be set aside for resumption upon resolution of another Future".
The first one is logically spelled "yield", the second one "await". But they could basically just be the same thing.
ChrisA