Skip to content

Conversation

@kavyasrinet
Copy link

@kavyasrinet kavyasrinet commented Feb 2, 2018

In addition to the PR: #7908 that covers the syntax for Select, this PR covers proposed Python syntax for Send/Recv operations.

ch = fluid.make_channel(dtype=INT)

# Writes and Reads are synchronous otherwise the calls will block.
ch.send(10)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this line will block forever, since no one is reading from another thread.

Copy link
Author

@kavyasrinet kavyasrinet Feb 3, 2018

Choose a reason for hiding this comment

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

You are right, I completely forgot to write threads for that. Just pushed it in a commit.

Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

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

LGTM!

Thanks @kavyasrinet !

@wangkuiyi wangkuiyi merged commit 497a131 into PaddlePaddle:develop Feb 5, 2018
@kavyasrinet kavyasrinet deleted the send_recv branch February 5, 2018 19:02
Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

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

I approved and merged this PR so I can edit it by myself.


def send_to_channel(channel, num_time=1):
for i in xrange(num_time):
channel.send(i)
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. The send operation must be a global function, but not the method of buffer, because it must be able to handle the case that the buffer is nil.
  2. The send operation has to be a fluid operator, not a Python function or method.
ch = fluid.make_channel(dtype=INT, buffer_size)

# Now write three elements to the channel
thread = threading.Thread(target=send_to_channel, args=(ch, 3, ))
Copy link
Collaborator

Choose a reason for hiding this comment

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

To check it works, we need fluid.go, instead of threading.


# Read all the data from the channel
for i in xrange(3):
y = ch.recv()
Copy link
Collaborator

Choose a reason for hiding this comment

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

y = fluid.recv(ch)

It doesn't help at all to expose the C++ method Channel::Recv to Python as Channel::recv.

y = ch.recv()

# Done receiving , now close the channel
ch.close()
Copy link
Collaborator

Choose a reason for hiding this comment

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

fluid.close(ch)

ch = fluid.make_channel(dtype=INT, buffer_size)

# Now write three elements to the channel
thread = threading.Thread(target=send_to_channel, args=(ch, 3, ))
Copy link
Collaborator

Choose a reason for hiding this comment

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

To illustrate how to use buffered channels, we don't need the goroutine, because the purpose is to show that the single thread example wouldn't block as long as the buffer size is big enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants