Rumah > Artikel > pembangunan bahagian belakang > 使用yield可以做哪些很酷的事情?
使用生成器(Generator)和yield可以做哪些有趣的、酷酷的、让人意想不到的事情?
不限编程语言,例如python、JavaScript 等。
<code class="language-text">function async(generator) {
return new Promise(function(resolve, reject) {
var g = generator()
function next(val) {
var result = g.next(val)
var value = result.value
if (!result.done) {
value.then(next).catch(reject)
}
else {
resolve(value)
}
}
next()
})
}
</code>
最典型的不就是async/await么?<code class="language-text">IEnumerable<action>> SomeAsyncMethod()
{
//blabla
yield return await( asyncMethod, context );
//blabla
yield return await( asyncMethod, context );
//blabla
}
</action></code>
可以做动画呀,效果如图:<code class="language-python"><span class="c"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">matplotlib.animation</span> <span class="kn">as</span> <span class="nn">animation</span>
<span class="kn">import</span> <span class="nn">math</span><span class="o">,</span> <span class="nn">random</span>
<span class="c"># 需要安装的库:Numpy和Matplotlib,推荐直接Anaconda</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axes1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="c"># 设置坐标轴长度</span>
<span class="n">axes1</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.4</span><span class="p">)</span>
<span class="n">axes1</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="mf">0.01</span><span class="p">)</span>
<span class="c"># 设置初始x、y数值数组</span>
<span class="n">xdata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
<span class="n">ydata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">xdata</span><span class="p">)</span>
<span class="c"># 获得线条</span>
<span class="n">line</span><span class="p">,</span> <span class="o">=</span> <span class="n">axes1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xdata</span><span class="p">)</span>
<span class="c"># 毛刺倍率,从0开始增长,offset越大毛刺越大</span>
<span class="n">offset</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="c">#因为update的参数是调用函数data_gen,所以第一个默认参数不能是framenum</span>
<span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">global</span> <span class="n">offset</span>
<span class="n">line</span><span class="o">.</span><span class="n">set_ydata</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">return</span> <span class="n">line</span><span class="p">,</span>
<span class="c"># 每次生成10个随机数据</span>
<span class="c"># 每次变化整幅图的话,yield一个整图就行了</span>
<span class="k">def</span> <span class="nf">data_gen</span><span class="p">():</span>
<span class="k">global</span> <span class="n">offset</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="n">length</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xdata</span><span class="p">))</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xdata</span><span class="p">)):</span>
<span class="n">ydata</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">math</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">xdata</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="o">+</span><span class="mf">0.2</span>
<span class="k">if</span> <span class="n">i</span><span class="o">></span><span class="n">length</span><span class="o">/</span><span class="mf">18.0</span> <span class="ow">and</span> <span class="n">i</span><span class="o"><span class="p">(</span><span class="n">length</span><span class="o">*</span><span class="mf">2.7</span><span class="o">/</span><span class="mf">6.0</span><span class="p">):</span>
<span class="n">ydata</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+=</span><span class="n">offset</span><span class="o">*</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span><span class="o">-</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mf">0.05</span>
<span class="c">#可以设置offset的最大值</span>
<span class="k">if</span> <span class="n">offset</span><span class="o">>=</span><span class="mf">0.5</span><span class="p">:</span>
<span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span>
<span class="k">yield</span> <span class="n">ydata</span>
<span class="c"># 配置完毕,开始播放</span>
<span class="n">ani</span> <span class="o">=</span> <span class="n">animation</span><span class="o">.</span><span class="n">FuncAnimation</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">update</span><span class="p">,</span> <span class="n">data_gen</span><span class="p">,</span> <span class="n">interval</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</span></code>
模拟离散事件,还有更简洁优雅的方式么<code class="language-python"><span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="n">iter_funcs</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">BATCH_SIZE</span><span class="p">):</span>
<span class="sd">"""Train the model with `dataset` with mini-batch training. Each</span>
<span class="sd"> mini-batch has `batch_size` recordings.</span>
<span class="sd"> """</span>
<span class="n">num_batches_train</span> <span class="o">=</span> <span class="n">dataset</span><span class="p">[</span><span class="s">'num_examples_train'</span><span class="p">]</span> <span class="o">//</span> <span class="n">batch_size</span>
<span class="n">num_batches_valid</span> <span class="o">=</span> <span class="n">dataset</span><span class="p">[</span><span class="s">'num_examples_valid'</span><span class="p">]</span> <span class="o">//</span> <span class="n">batch_size</span>
<span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
<span class="n">batch_train_losses</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_batches_train</span><span class="p">):</span>
<span class="n">batch_train_loss</span> <span class="o">=</span> <span class="n">iter_funcs</span><span class="p">[</span><span class="s">'train'</span><span class="p">](</span><span class="n">b</span><span class="p">)</span>
<span class="n">batch_train_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_train_loss</span><span class="p">)</span>
<span class="n">avg_train_loss</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_train_losses</span><span class="p">)</span>
<span class="n">batch_valid_losses</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">batch_valid_accuracies</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_batches_valid</span><span class="p">):</span>
<span class="n">batch_valid_loss</span><span class="p">,</span> <span class="n">batch_valid_accuracy</span> <span class="o">=</span> <span class="n">iter_funcs</span><span class="p">[</span><span class="s">'valid'</span><span class="p">](</span><span class="n">b</span><span class="p">)</span>
<span class="n">batch_valid_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_valid_loss</span><span class="p">)</span>
<span class="n">batch_valid_accuracies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_valid_accuracy</span><span class="p">)</span>
<span class="n">avg_valid_loss</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_valid_losses</span><span class="p">)</span>
<span class="n">avg_valid_accuracy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_valid_accuracies</span><span class="p">)</span>
<span class="k">yield</span> <span class="p">{</span>
<span class="s">'number'</span><span class="p">:</span> <span class="n">epoch</span><span class="p">,</span>
<span class="s">'train_loss'</span><span class="p">:</span> <span class="n">avg_train_loss</span><span class="p">,</span>
<span class="s">'valid_loss'</span><span class="p">:</span> <span class="n">avg_valid_loss</span><span class="p">,</span>
<span class="s">'valid_accuracy'</span><span class="p">:</span> <span class="n">avg_valid_accuracy</span><span class="p">,</span>
<span class="p">}</span>
</code>
tornado就是使用generator实现的协程(coroutine)模型,再配合event loop实现高并发的
使用迭代器遍历二叉树。