提交 f4d202ef 编写于 作者: T Travis CI

Deploy to GitHub Pages: 1ead6c26

上级 b4aebdf6
...@@ -159,6 +159,55 @@ ch.close() ...@@ -159,6 +159,55 @@ ch.close()
### Select ### Select
In Go, the `select` statement lets a goroutine wait on multiple communication operations. A `select` blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
```go
ch1 := make(chan int)
ch2 := make(chan int, 100)
x := 0
for {
select {
case ch1 <- x:
x := x + 1
case y <- ch2:
fmt.Println("Received on channel")
default:
fmt.Println("Default")
}
}
```
In Fluid, we should be able to do the same:
```python
ch1 = fluid.make_chan(dtype=INT)
ch2 = fluid.make_chan(dtype=INT, 100)
sel = fluid.select()
with sel.case(ch1, 'w', X):
fluid.layers.increment(X)
with sel.case(ch2, 'r', Y):
fluid.print("Received on Channel")
with sel.default():
fluid.print("Default")
```
In the above code snippet, `X` and `Y` are variables. Now let us look at each of these statements one by one.
- `sel.case(ch1, 'w', X)` : This specifies that we are writing to `ch1` and we want to write the integer in variable `X` to the channel. The character `w` is used here to make the syntax familar to write syntax in Python I/O.
- `sel.case(ch2, 'r', Y)` : This specifies that we would like to read the result from `ch2` into variable `Y`. The character `r` is used here to make the syntax familar to read syntax in Python I/O.
- `sel.default()` : This is equivalent to the default in Go `select`. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.
## Example Programs ## Example Programs
### 1. RPC between Trainers and Parameter Servers ### 1. RPC between Trainers and Parameter Servers
......
...@@ -341,6 +341,47 @@ ...@@ -341,6 +341,47 @@
</div> </div>
<div class="section" id="select"> <div class="section" id="select">
<span id="select"></span><h3>Select<a class="headerlink" href="#select" title="Permalink to this headline"></a></h3> <span id="select"></span><h3>Select<a class="headerlink" href="#select" title="Permalink to this headline"></a></h3>
<p>In Go, the <code class="docutils literal"><span class="pre">select</span></code> statement lets a goroutine wait on multiple communication operations. A <code class="docutils literal"><span class="pre">select</span></code> blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.</p>
<div class="highlight-go"><div class="highlight"><pre><span></span><span class="nx">ch1</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">)</span>
<span class="nx">ch2</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="nx">x</span> <span class="o">:=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="p">{</span>
<span class="k">select</span> <span class="p">{</span>
<span class="k">case</span> <span class="nx">ch1</span> <span class="o">&lt;-</span> <span class="nx">x</span><span class="p">:</span>
<span class="nx">x</span> <span class="o">:=</span> <span class="nx">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">case</span> <span class="nx">y</span> <span class="o">&lt;-</span> <span class="nx">ch2</span><span class="p">:</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Received on channel&quot;</span><span class="p">)</span>
<span class="k">default</span><span class="p">:</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Default&quot;</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In Fluid, we should be able to do the same:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">ch1</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">make_chan</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">INT</span><span class="p">)</span>
<span class="n">ch2</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">make_chan</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">INT</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="n">sel</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">select</span><span class="p">()</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">case</span><span class="p">(</span><span class="n">ch1</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
<span class="n">fluid</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">increment</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">case</span><span class="p">(</span><span class="n">ch2</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">Y</span><span class="p">):</span>
<span class="n">fluid</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s2">&quot;Received on Channel&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">default</span><span class="p">():</span>
<span class="n">fluid</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s2">&quot;Default&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above code snippet, <code class="docutils literal"><span class="pre">X</span></code> and <code class="docutils literal"><span class="pre">Y</span></code> are variables. Now let us look at each of these statements one by one.</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">sel.case(ch1,</span> <span class="pre">'w',</span> <span class="pre">X)</span></code> : This specifies that we are writing to <code class="docutils literal"><span class="pre">ch1</span></code> and we want to write the integer in variable <code class="docutils literal"><span class="pre">X</span></code> to the channel. The character <code class="docutils literal"><span class="pre">w</span></code> is used here to make the syntax familar to write syntax in Python I/O.</li>
<li><code class="docutils literal"><span class="pre">sel.case(ch2,</span> <span class="pre">'r',</span> <span class="pre">Y)</span></code> : This specifies that we would like to read the result from <code class="docutils literal"><span class="pre">ch2</span></code> into variable <code class="docutils literal"><span class="pre">Y</span></code>. The character <code class="docutils literal"><span class="pre">r</span></code> is used here to make the syntax familar to read syntax in Python I/O.</li>
<li><code class="docutils literal"><span class="pre">sel.default()</span></code> : This is equivalent to the default in Go <code class="docutils literal"><span class="pre">select</span></code>. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.</li>
</ul>
</div> </div>
</div> </div>
<div class="section" id="example-programs"> <div class="section" id="example-programs">
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -159,6 +159,55 @@ ch.close() ...@@ -159,6 +159,55 @@ ch.close()
### Select ### Select
In Go, the `select` statement lets a goroutine wait on multiple communication operations. A `select` blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
```go
ch1 := make(chan int)
ch2 := make(chan int, 100)
x := 0
for {
select {
case ch1 <- x:
x := x + 1
case y <- ch2:
fmt.Println("Received on channel")
default:
fmt.Println("Default")
}
}
```
In Fluid, we should be able to do the same:
```python
ch1 = fluid.make_chan(dtype=INT)
ch2 = fluid.make_chan(dtype=INT, 100)
sel = fluid.select()
with sel.case(ch1, 'w', X):
fluid.layers.increment(X)
with sel.case(ch2, 'r', Y):
fluid.print("Received on Channel")
with sel.default():
fluid.print("Default")
```
In the above code snippet, `X` and `Y` are variables. Now let us look at each of these statements one by one.
- `sel.case(ch1, 'w', X)` : This specifies that we are writing to `ch1` and we want to write the integer in variable `X` to the channel. The character `w` is used here to make the syntax familar to write syntax in Python I/O.
- `sel.case(ch2, 'r', Y)` : This specifies that we would like to read the result from `ch2` into variable `Y`. The character `r` is used here to make the syntax familar to read syntax in Python I/O.
- `sel.default()` : This is equivalent to the default in Go `select`. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.
## Example Programs ## Example Programs
### 1. RPC between Trainers and Parameter Servers ### 1. RPC between Trainers and Parameter Servers
......
...@@ -360,6 +360,47 @@ ...@@ -360,6 +360,47 @@
</div> </div>
<div class="section" id="select"> <div class="section" id="select">
<span id="select"></span><h3>Select<a class="headerlink" href="#select" title="永久链接至标题"></a></h3> <span id="select"></span><h3>Select<a class="headerlink" href="#select" title="永久链接至标题"></a></h3>
<p>In Go, the <code class="docutils literal"><span class="pre">select</span></code> statement lets a goroutine wait on multiple communication operations. A <code class="docutils literal"><span class="pre">select</span></code> blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.</p>
<div class="highlight-go"><div class="highlight"><pre><span></span><span class="nx">ch1</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">)</span>
<span class="nx">ch2</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">int</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="nx">x</span> <span class="o">:=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="p">{</span>
<span class="k">select</span> <span class="p">{</span>
<span class="k">case</span> <span class="nx">ch1</span> <span class="o">&lt;-</span> <span class="nx">x</span><span class="p">:</span>
<span class="nx">x</span> <span class="o">:=</span> <span class="nx">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">case</span> <span class="nx">y</span> <span class="o">&lt;-</span> <span class="nx">ch2</span><span class="p">:</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Received on channel&quot;</span><span class="p">)</span>
<span class="k">default</span><span class="p">:</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Default&quot;</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In Fluid, we should be able to do the same:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">ch1</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">make_chan</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">INT</span><span class="p">)</span>
<span class="n">ch2</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">make_chan</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">INT</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="n">sel</span> <span class="o">=</span> <span class="n">fluid</span><span class="o">.</span><span class="n">select</span><span class="p">()</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">case</span><span class="p">(</span><span class="n">ch1</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
<span class="n">fluid</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">increment</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">case</span><span class="p">(</span><span class="n">ch2</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">Y</span><span class="p">):</span>
<span class="n">fluid</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s2">&quot;Received on Channel&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="n">sel</span><span class="o">.</span><span class="n">default</span><span class="p">():</span>
<span class="n">fluid</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s2">&quot;Default&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above code snippet, <code class="docutils literal"><span class="pre">X</span></code> and <code class="docutils literal"><span class="pre">Y</span></code> are variables. Now let us look at each of these statements one by one.</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">sel.case(ch1,</span> <span class="pre">'w',</span> <span class="pre">X)</span></code> : This specifies that we are writing to <code class="docutils literal"><span class="pre">ch1</span></code> and we want to write the integer in variable <code class="docutils literal"><span class="pre">X</span></code> to the channel. The character <code class="docutils literal"><span class="pre">w</span></code> is used here to make the syntax familar to write syntax in Python I/O.</li>
<li><code class="docutils literal"><span class="pre">sel.case(ch2,</span> <span class="pre">'r',</span> <span class="pre">Y)</span></code> : This specifies that we would like to read the result from <code class="docutils literal"><span class="pre">ch2</span></code> into variable <code class="docutils literal"><span class="pre">Y</span></code>. The character <code class="docutils literal"><span class="pre">r</span></code> is used here to make the syntax familar to read syntax in Python I/O.</li>
<li><code class="docutils literal"><span class="pre">sel.default()</span></code> : This is equivalent to the default in Go <code class="docutils literal"><span class="pre">select</span></code>. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.</li>
</ul>
</div> </div>
</div> </div>
<div class="section" id="example-programs"> <div class="section" id="example-programs">
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册