python --- coroutine programming (use of third-party library gevent)

<div class="iteye-blog-content-contain" style="font-size: 14px"><p><div id="cnblogs_post_body"></p>
<p> <p>< ;span style="font-size: 16px">1. What is a coroutine? </span></p> </p>
<p> <p><span style="font-size: 16px"> Coroutine, also known as microthread. A coroutine is not a thread or a process, and its context switching is not controlled by the CPU. A coroutine is switched from the current task to other tasks controlled by the current task. A thread can contain multiple coroutines. For the CPU, there is no concept of coroutines. <strong>It is a lightweight user-mode thread (that is, only for users)</strong>. The coroutine has its own register context and stack. When the coroutine scheduling switches to other coroutines, the register context and stack are saved. When switching back to the current coroutine, the previously saved register context and stack are restored. </span></p> </p>
<p> <p><span style="font-size: 16px">2. Why use coroutines in programming? </span></p> </p>
<p> <p><span style="font-size: 16px"> The advantages of using coroutines: (1) The CPU does not need to bear the overhead of the context; (2) There is no need to lock (multiple threads) (3) The programmer switches the control flow to facilitate programming; (4) </span><span style="font-size: 16px"> High concurrency, high expansion, low Cost (a CPU supports tens of thousands of coroutines is not a problem). </span></p> </p>
<p> <p> &nbsp;<span style="font-size: 16px"> There are downsides. Disadvantages of coroutines: (1) The coroutine itself cannot use the CPU multi-core resources (unless it cooperates with multi-process or multi-threading); (2) encountering blocking operations will block the entire program. </span></p> </p>
<p> <p>&nbsp;</p> </p>
<p> <p>< ;span style="font-size: 16px">Example 1 (using yield to switch between tasks): </span></p>

<p>  <img id="code_img_closed_868047cc-5571-4a37-9978-938f91cb06d2" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /></p>
<p>  <img id="code_img_opened_868047cc-5571-4a37-9978-938f91cb06d2" class="code_img_opened" style="display: none" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /> </p>
<p>  <div id="cnblogs_code_open_868047cc-5571-4a37-9978-938f91cb06d2" class="cnblogs_code_hide"> </p>
<p>   <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time</p>
<p></span><span style="color: #008080"> 2</span> </p>
<p><span style="color: #008080"> 3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func1(name):</p>
<p></span><span style="color: #008080"> 4</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">----func1 start...----</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080"> 5</span>     <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(6<span style="color: #000000">):</p>
<p></span><span style="color: #008080"> 6</span>         temp = <span style="color: #0000ff">yield</span>      <span style="color: #008000">#</span><span style="color: #008000">每次遇到yield,func1在此处阻塞,直到temp接收到func2中con.send()传来的值</span></p>
<p><span style="color: #008080"> 7</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">%s in the func1</span><span style="color: #800000">&quot;</span> %<span style="color: #000000"> (str(temp)))</p>
<p></span><span style="color: #008080"> 8</span>         time.sleep(1<span style="color: #000000">)</p>
<p></span><span style="color: #008080"> 9</span> </p>
<p><span style="color: #008080">10</span> </p>
<p><span style="color: #008080">11</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func2():</p>
<p></span><span style="color: #008080">12</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">----func2 start...----</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">13</span>     con.<span style="color: #800080">__next__</span>()     <span style="color: #008000">#</span><span style="color: #008000">The real func1 call starts here</span></p>
<p><span style="color: #008080">14</span>     <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(5<span style="color: #000000">):</p>
<p></span><span style="color: #008080">15</span>         con.send(i+1<span style="color: #000000">)</p>
<p></span><span style="color: #008080">16</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">%s in the func2</span><span style="color: #800000">&quot;</span> %<span style="color: #000000"> i)</p>
<p></span><span style="color: #008080">17</span> </p>
<p><span style="color: #008080">18</span> </p>
<p><span style="color: #008080">19</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">:</p>
<p></span><span style="color: #008080">20</span>     con = func1(1)     <span style="color: #008000"># </span><span style="color: #008000">This is not a real function call in a function with yield, print con to know</span></p>







How should coroutines be used in ? </span></p> </p>
<p> <p><span style="font-size: 16px"> greenlet is a coroutine module implemented in C, Compared with the yield that comes with PYTHON , it allows you to switch between arbitrary functions without <strong>declaring this function as generator</strong> (con=func1(1) in example 1) just do this). </span></p> </p>
<p> <p>&nbsp;</p> </p>
<p> <p>< ;span style="font-size: 16px">Example 2: </span></p> </p>
<p> <div class="cnblogs_code"></p >
<p>  <img id="
<p>  <img id="code_img_opened_f0929bd7-dab0-49a3-8a44-d97faf62506a" class="code_img_opened" style="display: none" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /> </p>
<p>  <div id="cnblogs_code_open_f0929bd7-dab0-49a3-8a44-d97faf62506a" class="cnblogs_code_hide"> </p>
<p>   <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> greenlet</p>
<p></span><span style="color:#008080"> 2</span> </p>
<p><span style="color: #008080"> 3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func1():</p>
<p></span><span style="color: #008080"> 4</span>     <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(1,6<span style="color: #000000">):</p>
<p></span><span style="color: #008080"> 5</span>         <span style="color: #0000ff">print</span><span style="color: #000000">(i)</p>
<p></span><span style="color: #008080"> 6</span>        g2.switch()   <span style="color: #008000">#</span><span style="color: #008000">切换到g2</span></p>
<p><span style="color: #008080"> 7</span> </p>
<p><span style="color: #008080"> 8</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func2():</p>
<p></span><span style="color: #008080"> 9</span>     words = [<span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">b</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">c</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">d</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">e</span><span style="color: #800000">'</span><span style="color: #000000">]</p>color: #800000">e</span><span style="color: #800000">'</span><span style="color: #000000">]</p>color: #800000">e</span><span style="color: #800000">'</span><span style="color: #000000">]</p>
<p></span><span style="color: #008080">10</span>     <span style="color: #0000ff">for</span> w <span style="color: #0000ff">in</span><span style="color: #000000"> words:</p>
<p></span><span style="color: #008080">11</span>         <span style="color: #0000ff">print</span><span style="color: #000000">(w)</p>
<p></span><span style="color: #008080">12</span>         g1.switch()    <span style="color: #008000">#</span><span style="color: #008000">切换到g1</span></p>
<p><span style="color: #008080">13</span> </p>
<p><span style="color: #008080">14</span> g1 =<span style="color: #000000"> greenlet.greenlet(func1)</p>
<p></span><span style="color: #008080">15</span> g2 =<span style="color: #000000"> greenlet.greenlet(func2)</p>
<p></span><span style="color: #008080">16</span> g1.switch()   <span style="color: #008000">#</span><span style="color: #008000">切换到g1</span></pre> </p>
<p>  </div> </p>
<p>  <span class="cnblogs_code_collapse">Use greenlent module for task switching</span></p>
<p> </div> </p>
<p> <p> <span style="font-size: 15px"> Note: Using greenlent can easily switch between multitasking, but the most time-consuming program running is I/ O operation, to use coroutines to achieve high concurrency, it should be <strong> switch to other tasks once an I/O operation is encountered, and switch back to the current task after the I/O operation is completed (this process should be automatic ). </strong></span></p> </p>
<p> <p>&nbsp;</p> </p>
<p>  <p><span style="font-size: 16px">4. So in PYTHON , how to make tasks switch when encountering I/O operations? </span></p> </p>
<p> <p><span style="font-size: 16px"> We use the third-party library gevent for this. </span></p> </p>
<p> <p><span style="font-size: 16px">  gevent的官方定义:gevent is a&nbsp;<a class="reference external" href="https://en.wikipedia.org/wiki/Coroutine">coroutine</a>&nbsp;-based&nbsp;<a class="reference external" href="http:// PYTHON .org/"> PYTHON </a>&nbsp;networking library that uses&nbsp;<a class="reference external" href="https://greenlet.readthedocs.io/">greenlet</a>&nbsp;to provide a high-level synchronous API on top of the libev event loop.</span></p> </p>
<p> <p>&nbsp;</p> </p>
<p> <p><span style="font-size: 16px">例三(gevent的简单使用):</span></p> </p>
<p> <div class="cnblogs_code"></p>
<p>  <img id="code_img_closed_26c74430-ff34-4e2e-af26-e8a3c235b1f0" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /></p>
<p>  <img id="code_img_opened_26c74430-ff34-4e2e-af26-e8a3c235b1f0" class="code_img_opened" style="display: none" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /> </p>
<p>  <div id="cnblogs_code_open_26c74430-ff34-4e2e-af26-e8a3c235b1f0" class="cnblogs_code_hide"> </p>
<p>   <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> gevent</p>
<p></span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time</p>
<p></span><span style="color: #008080"> 3</span> </p>
<p><span style="color: #008080"> 4</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func1():</p>
<p></span><span style="color: #008080"> 5</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">func1: start.....</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080"> 6</span>     <span style="color: #008000">#</span><span style="color: #008000"> Put the current greenlet to sleep for at least *seconds*.(模拟I/O操作,任务在此处自动切换)</span></p>
<p><span style="color: #008080"> 7</span>     gevent.sleep(3<span style="color: #000000">)</p>
<p></span><span style="color: #008080"> 8</span>     <span style="color: #0000ff">print</span>(<span style="color:#800000">&quot;</span><span style="color: #800000">func1: end</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080"> 9</span> </p>
<p><span style="color: #008080">10</span> <span style="color: #0000ff">def</span><span style="color: #000000"> func2():</p>
<p></span><span style="color: #008080">11</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">func2: start.....</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">12</span>     gevent.sleep(0.5<span style="color: #000000">)</p>
<p></span><span style="color: #008080">13</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">func2: end</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">14</span> </p>
<p><span style="color: #008080">15</span> start_time =<span style="color: #000000"> time.time()</p>
<p></span><span style="color: #008080">16</span> <span style="color: #008000">#</span><span style="color: #008000"> joinall(greenlets, timeout=None, raise_error=False, count=None)</span></p>
<p><span style="color: #008080">17</span> <span style="color: #008000">#</span><span style="color: #008000"> Wait for the ``greenlets`` to finish.</span></p>
<p><span style="color: #008080">18</span> <span style="color: #008000">#</span><span style="color: #008000"> :return: A sequence of the greenlets that finished before the timeout (if any)expired.</span></p>
<p><span style="color: #008080">19</span> <span style="color: #000000">gevent.joinall([gevent.spawn(func1),</p>
<p></span><span style="color: #008080">20</span> <span style="color: #000000">                gevent.spawn(func2)])</p>
<p></span><span style="color: #008080">21</span> <span style="color: #008000">#</span><span style="color: #008000"> spawn(cls, *args, **kwargs)</span></p>
<p><span style="color: #008080">22</span> <span style="color: #008000">#</span><span style="color: #008000"> Create a new :class:`Greenlet` object and schedule it to run ``function(*args, **kwargs)``.</span></p>
<p><span style="color: #008080">23</span> <span style="color: #008000">#</span><span style="color:#008000"> This can be used as ``gevent.spawn`` or ``Greenlet.spawn``.</span></p>
<p><span style="color: #008080">24</span> </p>
<p><span style="color: #008080">25</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">cost:</span><span style="color: #800000">&quot;</span>, time.time()-<span style="color: #000000">start_time)</p>
<p></span><span style="color: #008080">26</span> <span style="color: #008000">#</span><span style="color: #008000"> 通过计算程序运行的时间可以发现程序确实是以单线程达模拟出了多任务并行的操作。</span></pre> </p>
<p>  </div> </p>
<p>  <span class="cnblogs_code_collapse">gevent的简单使用</span></p>
<p> </div> </p>
<p> <p>&nbsp;</p> </p>
<p> <p><span style="font-size: 16px">例四(gevent和urllib配合同时下载多个网页):</span></p> </p>
<p> <div class="cnblogs_code"></p>
<p>  <img id="code_img_closed_04c7985d-8718-4d81-b37a-847a2a20e510" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /></p>
<p>  <img id="code_img_opened_04c7985d-8718-4d81-b37a-847a2a20e510" class="code_img_opened" style="display: none" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /> </p>
<p>  <div id="cnblogs_code_open_04c7985d-8718-4d81-b37a-847a2a20e510" class="cnblogs_code_hide"> </p>
<p>   <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> urllib.request</p>
<p></span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> gevent,time</p>
<p></span><span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> gevent.monkey</p>
<p></span><span style="color: #008080"> 4</span> </p>
<p><span style="color: #008080"> 5</span> <span style="color: #0000ff">def</span> func(url=<span style="color: #800000">&quot;&quot;</span>, filename=<span style="color: #800000">&quot;&quot;</span><span style="color: #000000">):</p>
<p></span><span style="color: #008080"> 6</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">Download:%s</span><span style="color: #800000">&quot;</span> %<span style="color: #000000"> url)</p>
<p></span><span style="color: #008080"> 7</span>     result = urllib.request.urlopen(url)       <span style="color: #008000">#</span><span style="color: #008000">请求打开一个网页</span></p>
<p><span style="color: #008080"> 8</span>     data = result.read()     <span style="color: #008000">#</span><span style="color: #008000">读取内容</span></p>
<p><span style="color: #008080"> 9</span>     with open(filename, <span style="color: #800000">'</span><span style="color: #800000">wb</span><span style="color: #800000">'</span>) as fp:    <span style="color: #008000">#</span><span style="color: #008000">写入文档</span></p>
<p><span style="color: #008080">10</span> <span style="color: #000000">        fp.write(data)</p>
<p></span><span style="color: #008080">11</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">Finish:%s</span><span style="color: #800000">&quot;</span> %<span style="color: #000000"> url)</p>
<p></span><span style="color: #008080">12</span> </p>
<p><span style="color: #008080">13</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">&quot;</span><span style="color: #800000">__main__</span><span style="color: #800000">&quot;</span><span style="color: #000000">:</p>
<p></span><span style="color: #008080">14</span>     <span style="color: #008000">#</span><span style="color: #008000"> Do all of the default monkey patching (calls every other applicablefunction in this module).</span></p>
<p><span style="color: #008080">15</span>     <span style="color: #008000">#</span><span style="color: #008000"> 相当与做一个标记,做完此操作gevent就可以检测到此程序中所有的I/O操作</span></p>
<p><span style="color: #008080">16</span> <span style="color:#000000">    gevent.monkey.patch_all()</p>
<p></span><span style="color: #008080">17</span> </p>
<p><span style="color: #008080">18</span>     async_time =<span style="color: #000000"> time.time()</p>
<p></span><span style="color: #008080">19</span> <span style="color: #000000">    gevent.joinall([</p>
<p></span><span style="color: #008080">20</span>         gevent.spawn(func, <span style="color: #800000">&quot;</span><span style="color: #800000">http://www.cnblogs.com/ [url=http://www.cppentry.com/list.php?fid=77]PYTHON [/url]d-Li/p/7774497.html</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000">7774497.html</span><span style="color: #800000">&quot;</span><span style="color: #000000">),</p>
<p></span><span style="color: #008080">21</span>         gevent.spawn(func, <span style="color: #800000">&quot;</span><span style="color: #800000">http://www.gevent.org/</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000">gevent.html</span><span style="color: #800000">&quot;</span><span style="color: #000000">),</p>
<p></span><span style="color:#008080">22</span>         gevent.spawn(func, <span style="color: #800000">&quot;</span><span style="color: #800000">https://www. PYTHON .org/</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000"> PYTHON .html</span><span style="color: #800000">&quot;</span><span style="color: #000000">),</p>
<p></span><span style="color: #008080">23</span> <span style="color: #000000">    ])</p>
<p></span><span style="color: #008080">24</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">async download cost:</span><span style="color: #800000">&quot;</span>, time.time()-<span style="color: #000000">async_time)</p>
<p></span><span style="color: #008080">25</span> </p>
<p><span style="color: #008080">26</span>     start_time =<span style="color: #000000"> time.time()</p>
<p></span><span style="color: #008080">27</span>     func(<span style="color: #800000">&quot;</span><span style="color: #800000">http://www.cnblogs.com/ [url=http://www.cppentry.com/list.php?fid=77]PYTHON [/url]d-Li/p/7774497.html</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000">7774497.html</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">28</span>     func(<span style="color: #800000">&quot;</span><span style="color: #800000">http://www.gevent.org/</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000">gevent.html</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">29</span>     func(<span style="color: #800000">&quot;</span><span style="color: #800000">https://www. PYTHON .org/</span><span style="color: #800000">&quot;</span>, <span style="color: #800000">&quot;</span><span style="color: #800000"> PYTHON .html</span><span style="color: #800000">&quot;</span><span style="color: #000000">)</p>
<p></span><span style="color: #008080">30</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">download cost:</span><span style="color: #800000">&quot;</span>, time.time()-start_time)</pre> </p>
<p>  </div> </p>
<p>  <span class="cnblogs_code_collapse">gevent and urllib cooperate to download multiple pages at the same time</span></p>
<p> </div> </p>
< p> <p> Note: A simple multi-concurrency crawler can be implemented by slightly modifying the code of the above example and adding the parsing function of the html source code. </p> </p>
<p> <p>&nbsp;</p> </p>
<p> <p><span style=" font-size: 16px">Yes<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/ [url=http://www.cppentry.com/list. php?fid=77]PYTHON [/url]d-Li/p/7598850.html"> PYTHON --- Network Programming Socket</a> The socket_server2 in the second example can be transformed into a large concurrent socket server by using gevent.
</span></p> </p> <p> <p><span style="font-size: 16px">Example 5 (using gevent to implement concurrent socket server) : </span></p> </p>
<p> <div class="cnblogs_code"></p>
<p>  <img id="code_img_closed_45c1bc41-9757-42bd-8796-a6b582a5ef7c" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /></p>
<p>  <img id="code_img_opened_45c1bc41-9757-42bd-8796-a6b582a5ef7c" class="code_img_opened" style="display: none" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" /> </p>
<p>  <div id="cnblogs_code_open_45c1bc41-9757-42bd-8796-a6b582a5ef7c" class="cnblogs_code_hide"> </p>
<p>   <pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">服务端</span></p>
<p><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> socket</p>
<p></span><span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> gevent</p>
<p></span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> gevent.monkey</p>
<p></span><span style="color: #008080"> 5</span> </p>
<p><span style="color: #008080"> 6</span> <span style="color: #000000">gevent.monkey.patch_all()</p>
<p></span><span style="color: #008080"> 7</span> </p>
<p><span style="color: #008080"> 8</span> <span style="color: #0000ff">def</span><span style="color: #000000"> request_handler(conn):</p>
<p></span><span style="color: #008080"> 9</span> </p>
<p><span style="color: #008080">10</span>     <span style="color: #800000">'''</span></p>
<p><span style="color: #008080">11</span> <span style="color: #800000">    Wait for an incoming connection. Return a new socket</p>
<p></span><span style="color: #008080">12</span> <span style="color: #800000">    representing the connection, and the address of the client.</p>
<p></span><span style="color: #008080">13</span>     <span style="color: #800000">'''</span></p>
<p><span style="color: #008080">14</span>     <span style="color: #0000ff">while</span><span style="color: #000000"> True:</p>
<p></span><span style="color: #008080">15</span>         <span style="color: #008000">#</span><span style="color: #008000"> print(&quot;ok&quot;)</span></p>
<p><span style="color: #008080">16</span>         data = conn.recv(1024)         <span style="color: #008000">#</span><span style="color: #008000">接收信息,写明要接收信息的最大容量,单位为字节</span></p>
<p><span style="color: #008080">17</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">&quot;</span><span style="color: #800000">server recv:</span><span style="color: #800000">&quot;</span><span style="color: #000000">, data)</p>
<p></span><span style="color: #008080">18</span>         conn.send(data.upper())       <span style="color: #008000">#</span><span style="color: #008000">对收到的信息处理,返回到客户端</span></p>
<p><span style="color: #008080">19</span> </p>
<p><span style="color: #008080">20</span> </p>
<p><span style="color: #008080">21</span> </p>
<p><span style="color: #008080">22</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">&quot;</span><span style="color: #800000">__main__</span><span style="color: #800000">&quot;</span><span style="color: #000000">:</p>
<p></span><span style="color: #008080">23</span>     address = (<span style="color: #800000">&quot;</span><span style="color:#800000">localhost</span><span style="color: #800000">&quot;</span>, 6666)  <span style="color: #008000">#</span><span style="color: #008000"> 写明服务端要监听的地址,和端口号</span></p>
<p><span style="color: #008080">24</span>     server = socket.socket()  <span style="color: #008000">#</span><span style="color: #008000"> 生成一个socket对象</span></p>
<p><span style="color: #008080">25</span>     server.bind(address)  <span style="color: #008000">#</span><span style="color: #008000"> 用socket对象绑定要监听的地址和端口</span></p>
<p><span style="color: #008080">26</span>     server.listen()  <span style="color:#008000">#</span><span style="color: #008000"> 开始监听</span></p>
<p><span style="color: #008080">27</span> </p>
<p><span style="color: #008080">28</span>     <span style="color: #0000ff">while</span><span style="color: #000000"> True:</p>
<p></span><span style="color: #008080">29</span>         conn, addr = server.accept()  <span style="color: #008000">#</span><span style="color: #008000"> 等带新连接接入服务端,返回一个新的socket对象和地址,地址格式同前面格式</span></p>
<p><span style="color: #008080">30</span> <span style="color: #000000">       gevent.spawn(request_handler, conn)</p>
<p></span><span style="color: #008080">31</span> </p>
<p><span style="color: #008080">32</span>     server.close()  <span style="color: #008000">#</span><span style="color: #008000"> 关闭服务端</span></pre> </p>
<p>  </div> </p>
<p>  <span class="cnblogs_code_collapse">socket_server2的并发实现</span></p>
<p> </div> </p>
<p> <p>&nbsp;<span style="font-size: 16px">  注:可使用<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/ <p></div></p></div>--- Network Programming Socket</a> ;The socket_client2 of the second example is tested.</span></p></p> PYTHON[/url]d-Li/p/7598850.html"> [url=http://www.cppentry.com/list.php?fid=77]PYTHON

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396683&siteId=291194637