-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Rewrite the Send/Recv part of csp.md #8164
Conversation
doc/design/csp.md
Outdated
|
||
# Done receiving , now close the channel | ||
ch.close() | ||
fluid.close_channel(ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A side note, in Go, one can still receive valid value from a closed channel:
package main
import (
"fmt"
)
func main() {
a := make(chan int, 1)
a <- 5
close(a)
for {
v, ok := <-a
if !ok {
break
}
fmt.Println(v)
}
}
I think we will need similar functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right -- if there are any residual values left in a closed buffered channel, receive should be able to read them.
ch = fluid.make_channel(INT, 10)
fluid.send(ch, 1)
fluid.send(ch, 2)
fluid.send(ch, 3)
fluid.close(ch)
fluid.recv(ch) # true, 1
fluid.recv(ch) # true, 2
fluid.recv(ch) # true, 3
fluid.recv(ch) # false, 0
fluid.recv(ch) # false, 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to above,
- For unbuffered channels, the current behavior remains the same. We close everything and unblock everyone ?
- For buffered channels, a
send
operation afterfluid.close(ch)
should panic/ throw an error.
Correct me if I am wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added detailed explanations to this doc.
doc/design/csp.md
Outdated
|
||
# Done receiving , now close the channel | ||
ch.close() | ||
fluid.close_channel(ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically sender will close the channel, since send to a closed channel will panic, but receive from a closed channel will get zero value, optionally one can check if the value is valid:
a := make(chan int)
close(a)
fmt.Println(<-a) // 0
v, ok := <-a
fmt.Println(v, ok) // 0, false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
doc/design/csp.md
Outdated
thread.daemon = True | ||
thread.start() | ||
with fluid.while(steps=buffer_size): | ||
fluid.send(ch, step) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can reuse these methods as fluid.send()
and fluid.receive()
for reading and writing in select
cases as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we could. I don't have an idea how could we. Do you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like:
sel = fluid.select()
with sel.case(fluid.recv(ch), x): # similar to with sel.read_case(ch, x) or sel.case(ch.read(), x)
do_something_with(x)
As per the discussion in #8165
doc/design/csp.md
Outdated
|
||
# Done receiving , now close the channel | ||
ch.close() | ||
fluid.close_channel(ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to above,
- For unbuffered channels, the current behavior remains the same. We close everything and unblock everyone ?
- For buffered channels, a
send
operation afterfluid.close(ch)
should panic/ throw an error.
Correct me if I am wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Fix #8163