Modified: websites/staging/thrift/trunk/content/tutorial/py.html ============================================================================== --- websites/staging/thrift/trunk/content/tutorial/py.html (original) +++ websites/staging/thrift/trunk/content/tutorial/py.html Sat Mar 22 17:44:08 2014 @@ -87,118 +87,153 @@ </ol> <h3 id="prerequisites">Prerequisites</h3> <h3 id="client">Client</h3> -<p><div class="codehilite"><pre><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">glob</span> -<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'gen-py'</span><span class="p">)</span> -<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'../../lib/py/build/lib.*'</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span></p> -<p><span class="kn">from</span> <span class="nn">tutorial</span> <span class="kn">import</span> <span class="n">Calculator</span> -<span class="kn">from</span> <span class="nn">tutorial.ttypes</span> <span class="kn">import</span> <span class="o">*</span></p> -<p><span class="kn">from</span> <span class="nn">thrift</span> <span class="kn">import</span> <span class="n">Thrift</span> -<span class="kn">from</span> <span class="nn">thrift.transport</span> <span class="kn">import</span> <span class="n">TSocket</span> -<span class="kn">from</span> <span class="nn">thrift.transport</span> <span class="kn">import</span> <span class="n">TTransport</span> -<span class="kn">from</span> <span class="nn">thrift.protocol</span> <span class="kn">import</span> <span class="n">TBinaryProtocol</span></p> -<p><span class="k">try</span><span class="p">:</span></p> -<p><span class="c"># Make socket</span> - <span class="n">transport</span> <span class="o">=</span> <span class="n">TSocket</span><span class="o">.</span><span class="n">TSocket</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">,</span> <span class="mi">9090</span><span class="p">)</span></p> -<p><span class="c"># Buffering is critical. Raw sockets are very slow</span> - <span class="n">transport</span> <span class="o">=</span> <span class="n">TTransport</span><span class="o">.</span><span class="n">TBufferedTransport</span><span class="p">(</span><span class="n">transport</span><span class="p">)</span></p> -<p><span class="c"># Wrap in a protocol</span> - <span class="n">protocol</span> <span class="o">=</span> <span class="n">TBinaryProtocol</span><span class="o">.</span><span class="n">TBinaryProtocol</span><span class="p">(</span><span class="n">transport</span><span class="p">)</span></p> -<p><span class="c"># Create a client to use the protocol encoder</span> - <span class="n">client</span> <span class="o">=</span> <span class="n">Calculator</span><span class="o">.</span><span class="n">Client</span><span class="p">(</span><span class="n">protocol</span><span class="p">)</span></p> -<p><span class="c"># Connect!</span> - <span class="n">transport</span><span class="o">.</span><span class="n">open</span><span class="p">()</span></p> -<p><span class="n">client</span><span class="o">.</span><span class="n">ping</span><span class="p">()</span> - <span class="k">print</span> <span class="s">'ping()'</span></p> -<p><span class="nb">sum</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> - <span class="k">print</span> <span class="s">'1+1=</span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="nb">sum</span><span class="p">)</span></p> -<p><span class="n">work</span> <span class="o">=</span> <span class="n">Work</span><span class="p">()</span></p> -<p><span class="n">work</span><span class="o">.</span><span class="n">op</span> <span class="o">=</span> <span class="n">Operation</span><span class="o">.</span><span class="n">DIVIDE</span> - <span class="n">work</span><span class="o">.</span><span class="n">num1</span> <span class="o">=</span> <span class="mi">1</span> - <span class="n">work</span><span class="o">.</span><span class="n">num2</span> <span class="o">=</span> <span class="mi">0</span></p> -<p><span class="k">try</span><span class="p">:</span> - <span class="n">quotient</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">work</span><span class="p">)</span> - <span class="k">print</span> <span class="s">'Whoa? You know how to divide by zero?'</span> - <span class="k">except</span> <span class="n">InvalidOperation</span><span class="p">,</span> <span class="n">io</span><span class="p">:</span> - <span class="k">print</span> <span class="s">'InvalidOperation: </span><span class="si">%r</span><span class="s">'</span> <span class="o">%</span> <span class="n">io</span></p> -<p><span class="n">work</span><span class="o">.</span><span class="n">op</span> <span class="o">=</span> <span class="n">Operation</span><span class="o">.</span><span class="n">SUBTRACT</span> - <span class="n">work</span><span class="o">.</span><span class="n">num1</span> <span class="o">=</span> <span class="mi">15</span> - <span class="n">work</span><span class="o">.</span><span class="n">num2</span> <span class="o">=</span> <span class="mi">10</span></p> -<p><span class="n">diff</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">work</span><span class="p">)</span> - <span class="k">print</span> <span class="s">'15-10=</span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">diff</span><span class="p">)</span></p> -<p><span class="n">log</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">getStruct</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> - <span class="k">print</span> <span class="s">'Check log: </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">log</span><span class="o">.</span><span class="n">value</span><span class="p">)</span></p> -<p><span class="c"># Close!</span> - <span class="n">transport</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></p> -<p><span class="k">except</span> <span class="n">Thrift</span><span class="o">.</span><span class="n">TException</span><span class="p">,</span> <span class="n">tx</span><span class="p">:</span> - <span class="k">print</span> <span class="s">'</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">tx</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> -</pre></div></p> +<div class="codehilite"><pre> <span class="p">:::</span><span class="n">python</span> +<span class="n">import</span> <span class="n">sys</span><span class="p">,</span> <span class="n">glob</span> +<span class="n">sys</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">gen</span><span class="o">-</span><span class="n">py</span><span class="o">&</span>#39<span class="p">;)</span> +<span class="n">sys</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span>0<span class="p">,</span> <span class="n">glob</span><span class="p">.</span><span class="n">glob</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;.</span><span class="o">./</span><span class="p">.</span><span class="o">./</span><span class="n">lib</span><span class="o">/</span><span class="n">py</span><span class="o">/</span><span class="n">build</span><span class="o">/</span><span class="n">lib</span><span class="o">.*&</span>#39<span class="p">;)[</span>0<span class="p">])</span> + +<span class="n">from</span> <span class="n">tutorial</span> <span class="n">import</span> <span class="n">Calculator</span> +<span class="n">from</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">ttypes</span> <span class="n">import</span> <span class="o">*</span> + +<span class="n">from</span> <span class="n">thrift</span> <span class="n">import</span> <span class="n">Thrift</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span> <span class="n">import</span> <span class="n">TSocket</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span> <span class="n">import</span> <span class="n">TTransport</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">protocol</span> <span class="n">import</span> <span class="n">TBinaryProtocol</span> + +<span class="k">try</span><span class="p">:</span> + + # <span class="n">Make</span> <span class="n">socket</span> + <span class="n">transport</span> <span class="p">=</span> <span class="n">TSocket</span><span class="p">.</span><span class="n">TSocket</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">localhost</span><span class="o">&</span>#39<span class="p">;,</span> 9090<span class="p">)</span> + + # <span class="n">Buffering</span> <span class="n">is</span> <span class="n">critical</span><span class="p">.</span> <span class="n">Raw</span> <span class="n">sockets</span> <span class="n">are</span> <span class="n">very</span> <span class="n">slow</span> + <span class="n">transport</span> <span class="p">=</span> <span class="n">TTransport</span><span class="p">.</span><span class="n">TBufferedTransport</span><span class="p">(</span><span class="n">transport</span><span class="p">)</span> + + # <span class="n">Wrap</span> <span class="n">in</span> <span class="n">a</span> <span class="n">protocol</span> + <span class="n">protocol</span> <span class="p">=</span> <span class="n">TBinaryProtocol</span><span class="p">.</span><span class="n">TBinaryProtocol</span><span class="p">(</span><span class="n">transport</span><span class="p">)</span> + + # <span class="n">Create</span> <span class="n">a</span> <span class="n">client</span> <span class="n">to</span> <span class="n">use</span> <span class="n">the</span> <span class="n">protocol</span> <span class="n">encoder</span> + <span class="n">client</span> <span class="p">=</span> <span class="n">Calculator</span><span class="p">.</span><span class="n">Client</span><span class="p">(</span><span class="n">protocol</span><span class="p">)</span> + + # <span class="n">Connect</span>! + <span class="n">transport</span><span class="p">.</span><span class="n">open</span><span class="p">()</span> + + <span class="n">client</span><span class="p">.</span><span class="n">ping</span><span class="p">()</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">ping</span><span class="p">()</span><span class="o">&</span>#39<span class="p">;</span> + + <span class="n">sum</span> <span class="p">=</span> <span class="n">client</span><span class="p">.</span><span class="n">add</span><span class="p">(</span>1<span class="p">,</span>1<span class="p">)</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span>1<span class="o">+</span>1<span class="p">=</span><span class="c">%d&#39; % (sum)</span> + + <span class="n">work</span> <span class="p">=</span> <span class="n">Work</span><span class="p">()</span> + + <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="p">=</span> <span class="n">Operation</span><span class="p">.</span><span class="n">DIVIDE</span> + <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="p">=</span> 1 + <span class="n">work</span><span class="p">.</span><span class="n">num2</span> <span class="p">=</span> 0 + + <span class="k">try</span><span class="p">:</span> + <span class="n">quotient</span> <span class="p">=</span> <span class="n">client</span><span class="p">.</span><span class="n">calculate</span><span class="p">(</span>1<span class="p">,</span> <span class="n">work</span><span class="p">)</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">Whoa</span>? <span class="n">You</span> <span class="n">know</span> <span class="n">how</span> <span class="n">to</span> <span class="n">divide</span> <span class="n">by</span> <span class="n">zero</span>?<span class="o">&</span>#39<span class="p">;</span> + <span class="n">except</span> <span class="n">InvalidOperation</span><span class="p">,</span> <span class="n">io</span><span class="p">:</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">InvalidOperation</span><span class="p">:</span> <span class="c">%r&#39; % io</span> + + <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="p">=</span> <span class="n">Operation</span><span class="p">.</span><span class="n">SUBTRACT</span> + <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="p">=</span> 15 + <span class="n">work</span><span class="p">.</span><span class="n">num2</span> <span class="p">=</span> 10 + + <span class="n">diff</span> <span class="p">=</span> <span class="n">client</span><span class="p">.</span><span class="n">calculate</span><span class="p">(</span>1<span class="p">,</span> <span class="n">work</span><span class="p">)</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span>15<span class="o">-</span>10<span class="p">=</span><span class="c">%d&#39; % (diff)</span> + + <span class="nb">log</span> <span class="p">=</span> <span class="n">client</span><span class="p">.</span><span class="n">getStruct</span><span class="p">(</span>1<span class="p">)</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">Check</span> <span class="nb">log</span><span class="p">:</span> <span class="c">%s&#39; % (log.value)</span> + + # <span class="n">Close</span>! + <span class="n">transport</span><span class="p">.</span><span class="n">close</span><span class="p">()</span> + +<span class="n">except</span> <span class="n">Thrift</span><span class="p">.</span><span class="n">TException</span><span class="p">,</span> <span class="n">tx</span><span class="p">:</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="c">%s&#39; % (tx.message)</span> +</pre></div> + + <h3 id="server">Server</h3> -<p><div class="codehilite"><pre><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">glob</span> -<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'gen-py'</span><span class="p">)</span> -<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'../../lib/py/build/lib.*'</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span></p> -<p><span class="kn">from</span> <span class="nn">tutorial</span> <span class="kn">import</span> <span class="n">Calculator</span> -<span class="kn">from</span> <span class="nn">tutorial.ttypes</span> <span class="kn">import</span> <span class="o">*</span></p> -<p><span class="kn">from</span> <span class="nn">shared.ttypes</span> <span class="kn">import</span> <span class="n">SharedStruct</span></p> -<p><span class="kn">from</span> <span class="nn">thrift.transport</span> <span class="kn">import</span> <span class="n">TSocket</span> -<span class="kn">from</span> <span class="nn">thrift.transport</span> <span class="kn">import</span> <span class="n">TTransport</span> -<span class="kn">from</span> <span class="nn">thrift.protocol</span> <span class="kn">import</span> <span class="n">TBinaryProtocol</span> -<span class="kn">from</span> <span class="nn">thrift.server</span> <span class="kn">import</span> <span class="n">TServer</span></p> -<p><span class="k">class</span> <span class="nc">CalculatorHandler</span><span class="p">:</span> - <span class="k">def</span> <span class="nf"><strong>init</strong></span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> - <span class="bp">self</span><span class="o">.</span><span class="n">log</span> <span class="o">=</span> <span class="p">{}</span></p> -<p><span class="k">def</span> <span class="nf">ping</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> - <span class="k">print</span> <span class="s">'ping()'</span></p> -<p><span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">):</span> - <span class="k">print</span> <span class="s">'add(</span><span class="si">%d</span><span class="s">,</span><span class="si">%d</span><span class="s">)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">)</span> - <span class="k">return</span> <span class="n">n1</span><span class="o">+</span><span class="n">n2</span></p> -<p><span class="k">def</span> <span class="nf">calculate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logid</span><span class="p">,</span> <span class="n">work</span><span class="p">):</span> - <span class="k">print</span> <span class="s">'calculate(</span><span class="si">%d</span><span class="s">, </span><span class="si">%r</span><span class="s">)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">logid</span><span class="p">,</span> <span class="n">work</span><span class="p">)</span></p> -<div class="codehilite"><pre><span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>if<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>op<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>ADD<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>+<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>elif<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>op<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</sp an><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>SUBTRACT<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>-<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>elif<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>op<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</sp an><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>MULTIPLY<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>*<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>elif<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>op<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>Operation<span class="nt"></span><span</span> <span class="na">class=</sp an><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>DIVIDE<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>if<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>==<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"mi"</span><span class="nt">></span>0<span class="nt"></span><span</span> <span class="na">class=</span><s pan class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>InvalidOperation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>()<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>what<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n" ;</span><span class="nt">></span>op<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>why<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"s"</span><span class="nt">></span><span class="ni">&#39;</span>Cannot divide by 0<span class="ni">&#39;</span><span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>raise<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num1<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>/<span class="nt"></span></span> <span class="nt"><span</span> <span cl ass="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>num2<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>else<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>:<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>InvalidOperation<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>()<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>what<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>work<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"< /span><span class="nt">></span>op<span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>why<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"s"</span><span class="nt">></span><span class="ni">&#39;</span>Invalid operation<span class="ni">&#39;</span><span class="nt"></span></span> - <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>raise<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>x<span class="nt"></span></span> - -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>SharedStruct<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>()<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>key<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>logid<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>value<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"s"</span><span class="nt">></span><span class="ni">&#39;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"si"</span><span class="nt">></span>%d<span class="nt"></span><span</span> <span class="na">class=</spa n><span class="s">"s"</span><span class="nt">></span><span class="ni">&#39;</span><span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>%<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>(<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>)<span class="nt"></span></span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"bp"</span><span class="nt">></span>self<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>.<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>[<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>logid<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>]<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt"> ></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>log<span class="nt"></span></span> +<div class="codehilite"><pre> <span class="p">:::</span><span class="n">python</span> +<span class="n">import</span> <span class="n">sys</span><span class="p">,</span> <span class="n">glob</span> +<span class="n">sys</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">gen</span><span class="o">-</span><span class="n">py</span><span class="o">&</span>#39<span class="p">;)</span> +<span class="n">sys</span><span class="p">.</span><span class="n">path</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span>0<span class="p">,</span> <span class="n">glob</span><span class="p">.</span><span class="n">glob</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;.</span><span class="o">./</span><span class="p">.</span><span class="o">./</span><span class="n">lib</span><span class="o">/</span><span class="n">py</span><span class="o">/</span><span class="n">build</span><span class="o">/</span><span class="n">lib</span><span class="o">.*&</span>#39<span class="p">;)[</span>0<span class="p">])</span> + +<span class="n">from</span> <span class="n">tutorial</span> <span class="n">import</span> <span class="n">Calculator</span> +<span class="n">from</span> <span class="n">tutorial</span><span class="p">.</span><span class="n">ttypes</span> <span class="n">import</span> <span class="o">*</span> + +<span class="n">from</span> <span class="n">shared</span><span class="p">.</span><span class="n">ttypes</span> <span class="n">import</span> <span class="n">SharedStruct</span> + +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span> <span class="n">import</span> <span class="n">TSocket</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">transport</span> <span class="n">import</span> <span class="n">TTransport</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">protocol</span> <span class="n">import</span> <span class="n">TBinaryProtocol</span> +<span class="n">from</span> <span class="n">thrift</span><span class="p">.</span><span class="n">server</span> <span class="n">import</span> <span class="n">TServer</span> + +<span class="n">class</span> <span class="n">CalculatorHandler</span><span class="p">:</span> + <span class="n">def</span> <span class="n">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> + <span class="n">self</span><span class="p">.</span><span class="nb">log</span> <span class="p">=</span> <span class="p">{}</span> + + <span class="n">def</span> <span class="n">ping</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">ping</span><span class="p">()</span><span class="o">&</span>#39<span class="p">;</span> + + <span class="n">def</span> <span class="n">add</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">):</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">add</span><span class="p">(</span><span class="c">%d,%d)&#39; % (n1, n2)</span> + <span class="k">return</span> <span class="n">n1</span><span class="o">+</span><span class="n">n2</span> + + <span class="n">def</span> <span class="n">calculate</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">logid</span><span class="p">,</span> <span class="n">work</span><span class="p">):</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">calculate</span><span class="p">(</span><span class="c">%d, %r)&#39; % (logid, work)</span> + + <span class="k">if</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">==</span> <span class="n">Operation</span><span class="p">.</span><span class="n">ADD</span><span class="p">:</span> + <span class="n">val</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">+</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span> + <span class="n">elif</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">==</span> <span class="n">Operation</span><span class="p">.</span><span class="n">SUBTRACT</span><span class="p">:</span> + <span class="n">val</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">-</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span> + <span class="n">elif</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">==</span> <span class="n">Operation</span><span class="p">.</span><span class="n">MULTIPLY</span><span class="p">:</span> + <span class="n">val</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">*</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span> + <span class="n">elif</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> <span class="o">==</span> <span class="n">Operation</span><span class="p">.</span><span class="n">DIVIDE</span><span class="p">:</span> + <span class="k">if</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span> <span class="o">==</span> 0<span class="p">:</span> + <span class="n">x</span> <span class="p">=</span> <span class="n">InvalidOperation</span><span class="p">()</span> + <span class="n">x</span><span class="p">.</span><span class="n">what</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> + <span class="n">x</span><span class="p">.</span><span class="nb">why</span> <span class="p">=</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">Cannot</span> <span class="n">divide</span> <span class="n">by</span> 0<span class="o">&</span>#39<span class="p">;</span> + <span class="n">raise</span> <span class="n">x</span> + <span class="n">val</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">num1</span> <span class="o">/</span> <span class="n">work</span><span class="p">.</span><span class="n">num2</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">x</span> <span class="p">=</span> <span class="n">InvalidOperation</span><span class="p">()</span> + <span class="n">x</span><span class="p">.</span><span class="n">what</span> <span class="p">=</span> <span class="n">work</span><span class="p">.</span><span class="n">op</span> + <span class="n">x</span><span class="p">.</span><span class="nb">why</span> <span class="p">=</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">Invalid</span> <span class="n">operation</span><span class="o">&</span>#39<span class="p">;</span> + <span class="n">raise</span> <span class="n">x</span> + + <span class="nb">log</span> <span class="p">=</span> <span class="n">SharedStruct</span><span class="p">()</span> + <span class="nb">log</span><span class="p">.</span><span class="n">key</span> <span class="p">=</span> <span class="n">logid</span> + <span class="nb">log</span><span class="p">.</span><span class="n">value</span> <span class="p">=</span> <span class="o">&</span>#39<span class="p">;</span><span class="c">%d&#39; % (val)</span> + <span class="n">self</span><span class="p">.</span><span class="nb">log</span><span class="p">[</span><span class="n">logid</span><span class="p">]</span> <span class="p">=</span> <span class="nb">log</span> + + <span class="k">return</span> <span class="n">val</span> + + <span class="n">def</span> <span class="n">getStruct</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">getStruct</span><span class="p">(</span><span class="c">%d)&#39; % (key)</span> + <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="nb">log</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> + + <span class="n">def</span> <span class="n">zip</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> + <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">zip</span><span class="p">()</span><span class="o">&</span>#39<span class="p">;</span> + +<span class="n">handler</span> <span class="p">=</span> <span class="n">CalculatorHandler</span><span class="p">()</span> +<span class="n">processor</span> <span class="p">=</span> <span class="n">Calculator</span><span class="p">.</span><span class="n">Processor</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span> +<span class="n">transport</span> <span class="p">=</span> <span class="n">TSocket</span><span class="p">.</span><span class="n">TServerSocket</span><span class="p">(</span><span class="n">port</span><span class="p">=</span>9090<span class="p">)</span> +<span class="n">tfactory</span> <span class="p">=</span> <span class="n">TTransport</span><span class="p">.</span><span class="n">TBufferedTransportFactory</span><span class="p">()</span> +<span class="n">pfactory</span> <span class="p">=</span> <span class="n">TBinaryProtocol</span><span class="p">.</span><span class="n">TBinaryProtocolFactory</span><span class="p">()</span> + +<span class="n">server</span> <span class="p">=</span> <span class="n">TServer</span><span class="p">.</span><span class="n">TSimpleServer</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">transport</span><span class="p">,</span> <span class="n">tfactory</span><span class="p">,</span> <span class="n">pfactory</span><span class="p">)</span> + +# <span class="n">You</span> <span class="n">could</span> <span class="n">do</span> <span class="n">one</span> <span class="n">of</span> <span class="n">these</span> <span class="k">for</span> <span class="n">a</span> <span class="n">multithreaded</span> <span class="n">server</span> +#<span class="n">server</span> <span class="p">=</span> <span class="n">TServer</span><span class="p">.</span><span class="n">TThreadedServer</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">transport</span><span class="p">,</span> <span class="n">tfactory</span><span class="p">,</span> <span class="n">pfactory</span><span class="p">)</span> +#<span class="n">server</span> <span class="p">=</span> <span class="n">TServer</span><span class="p">.</span><span class="n">TThreadPoolServer</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">transport</span><span class="p">,</span> <span class="n">tfactory</span><span class="p">,</span> <span class="n">pfactory</span><span class="p">)</span> -<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>return<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"n"</span><span class="nt">></span>val<span class="nt"></span></span> +<span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">Starting</span> <span class="n">the</span> <span class="n">server</span><span class="p">...</span><span class="o">&</span>#39<span class="p">;</span> +<span class="n">server</span><span class="p">.</span><span class="n">serve</span><span class="p">()</span> +<span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">done</span><span class="p">.</span><span class="o">&</span>#39<span class="p">;</span> </pre></div> -<p><span class="k">def</span> <span class="nf">getStruct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span> - <span class="k">print</span> <span class="s">'getStruct(</span><span class="si">%d</span><span class="s">)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">)</span> - <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="p">[</span><span class="n">key</span><span class="p">]</span></p> -<p><span class="k">def</span> <span class="nf">zip</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> - <span class="k">print</span> <span class="s">'zip()'</span></p> -<p><span class="n">handler</span> <span class="o">=</span> <span class="n">CalculatorHandler</span><span class="p">()</span> -<span class="n">processor</span> <span class="o">=</span> <span class="n">Calculator</span><span class="o">.</span><span class="n">Processor</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span> -<span class="n">transport</span> <span class="o">=</span> <span class="n">TSocket</span><span class="o">.</span><span class="n">TServerSocket</span><span class="p">(</span><span class="n">port</span><span class="o">=</span><span class="mi">9090</span><span class="p">)</span> -<span class="n">tfactory</span> <span class="o">=</span> <span class="n">TTransport</span><span class="o">.</span><span class="n">TBufferedTransportFactory</span><span class="p">()</span> -<span class="n">pfactory</span> <span class="o">=</span> <span class="n">TBinaryProtocol</span><span class="o">.</span><span class="n">TBinaryProtocolFactory</span><span class="p">()</span></p> -<p><span class="n">server</span> <span class="o">=</span> <span class="n">TServer</span><span class="o">.</span><span class="n">TSimpleServer</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">transport</span><span class="p">,</span> <span class="n">tfactory</span><span class="p">,</span> <span class="n">pfactory</span><span class="p">)</span></p> -<p><span class="c"># You could do one of these for a multithreaded server</span> -<span class="c">#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)</span> -<span class="c">#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)</span></p> -<p><span class="k">print</span> <span class="s">'Starting the server...'</span> -<span class="n">server</span><span class="o">.</span><span class="n">serve</span><span class="p">()</span> -<span class="k">print</span> <span class="s">'done.'</span> -</pre></div></p> <h2 id="additional-information">Additional Information</h2> </div>