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

Rewrite the Send/Recv part of csp.md #8164

Merged
merged 3 commits into from
Feb 6, 2018
Merged

Rewrite the Send/Recv part of csp.md #8164

merged 3 commits into from
Feb 6, 2018

Conversation

wangkuiyi
Copy link
Collaborator

Fix #8163


# Done receiving , now close the channel
ch.close()
fluid.close_channel(ch)
Copy link
Contributor

@helinwang helinwang Feb 5, 2018

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.

Copy link
Collaborator Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to above,

  1. For unbuffered channels, the current behavior remains the same. We close everything and unblock everyone ?
  2. For buffered channels, a send operation after fluid.close(ch) should panic/ throw an error.

Correct me if I am wrong.

Copy link
Collaborator Author

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.


# Done receiving , now close the channel
ch.close()
fluid.close_channel(ch)
Copy link
Contributor

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

kavyasrinet
kavyasrinet previously approved these changes Feb 5, 2018
Copy link

@kavyasrinet kavyasrinet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

thread.daemon = True
thread.start()
with fluid.while(steps=buffer_size):
fluid.send(ch, step)

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.

Copy link
Collaborator Author

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?

Copy link

@kavyasrinet kavyasrinet Feb 5, 2018

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


# Done receiving , now close the channel
ch.close()
fluid.close_channel(ch)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to above,

  1. For unbuffered channels, the current behavior remains the same. We close everything and unblock everyone ?
  2. For buffered channels, a send operation after fluid.close(ch) should panic/ throw an error.

Correct me if I am wrong.

helinwang
helinwang previously approved these changes Feb 5, 2018
Copy link
Contributor

@helinwang helinwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@wangkuiyi wangkuiyi merged commit b0ecb36 into develop Feb 6, 2018
@luotao1 luotao1 deleted the wangkuiyi-patch-1 branch February 7, 2018 13:59
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

Successfully merging this pull request may close these issues.

3 participants