<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/category/proglanguage/erlang/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Tue, 10 Nov 2009 16:36:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>Erlang和Python互通</title>
		<link>https://coolshell.cn/articles/1313.html</link>
					<comments>https://coolshell.cn/articles/1313.html#comments</comments>
		
		<dc:creator><![CDATA[free.wang]]></dc:creator>
		<pubDate>Mon, 24 Aug 2009 14:30:49 +0000</pubDate>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[编程语言]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1313</guid>

					<description><![CDATA[<p>最近开发 Erlang ,对其字符串处理能力无言至极,于是决定把它和python联合起来,打造一个强力的分布式系统,等将来需要系统级开发时,我再把 C++/C组...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1313.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1313.html">Erlang和Python互通</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>最近开发 Erlang ,对其字符串处理能力无言至极,于是决定把它和python联合起来,打造一个强力的分布式系统,等将来需要系统级开发时,我再把 C++/C组合进来.</p>
<p>首先参考了 Erlang 官方文档和 <a href="http://www.zend2.com/DoIt.php?u=Oi8vd3d3LmJsb2dnZXIuY29tL2Jsb2cuZGV2ZWxvcGVycy5hcGkuc2luYS5jb20uY24vP3RhZz1lcmxhbmc%3D&amp;b=5">http://<cite>blog.developers.api.sina.com.cn/?tag=<strong>erlang</strong></cite></a> 以及<a href="http://www.zend2.com/DoIt.php?u=Oi8va2F6bWllci5uZXQvY29tcHV0ZXIvcG9ydC1ob3d0by8%3D&amp;b=5"> http://kazmier.net/computer/port-howto/</a> .</p>
<p>研读了将近24个小时, 才终于完全把问题解决.  起名为town，town在英文里表示集市，也就是代表各种语言在这里的交流与互动。) )<br />
<span id="more-1313"></span><br />
[erl]-module(town).<br />
-behaviour(gen_server).</p>
<p>%% API<br />
-export([start/0,combine/1]).</p>
<p>%% gen_server callbacks<br />
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,<br />
terminate/2, code_change/3]).<br />
-record(state, {port}).</p>
<p>start() -&amp;gt;<br />
  gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).<br />
stop() -&amp;gt;<br />
  gen_server:cast(?SERVER, stop).<br />
init([]) -&amp;gt;<br />
  process_flag(trap_exit, true),<br />
  Port = open_port({spawn, &quot;python -u /home/freefis/Desktop/town.py&quot;},[stream,{line, 1024}]),<br />
  {ok, #state{port = Port}}.</p>
<p>handle_call({combine,String}, _From, #state{port = Port} = State) -&amp;gt;<br />
  port_command(Port,String),<br />
  receive<br />
    {Port,{data,{_Flag,Data}}} -&amp;gt;<br />
      io:format(&quot;receiving:~p~n&quot;,[Data]),<br />
      sleep(2000),<br />
      {reply, Data, Port}<br />
  end.<br />
handle_cast(stop, State) -&amp;gt;<br />
  {stop, normal, State};<br />
handle_cast(_Msg, State) -&amp;gt;<br />
  {noreply, State}.</p>
<p>handle_info(Info, State) -&amp;gt;<br />
  {noreply,State}.</p>
<p>terminate(_Reason, Port) -&amp;gt;<br />
  ok.</p>
<p>code_change(_OldVsn, State, _Extra) -&amp;gt;<br />
  {ok, State}.</p>
<p>%%&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
%%% Internal &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
combine(_String) -&amp;gt;<br />
  start(),<br />
  String = list_to_binary(&quot;combine|&quot;++_String++&quot;\n&quot;),<br />
  gen_server:call(?SERVER,{combine,String},infinity),<br />
  stop().[/erl]<br />
这段是Python的脚本 当erlang中town:combine(&#8220;sentence1+sentence2&#8221;)执行时，会在后台启动python的脚本，处理完毕后返回给Erlang结果:sentence1sentence2，然后退出。 </p>
<pre data-enlighter-language="python" class="EnlighterJSRAW">
import sys
def handle(_string):
    if _string.startswith(&quot;combine|&quot;):
        string = &quot;&quot;.join( _string[8:].split(&quot;,&quot;))
        return string

&quot;&quot;&quot;waiting for input &quot;&quot;&quot;
while 1:
    # Recv. Binary Stream as Standard IN
    _stream = sys.stdin.readline()

if not _stream: break
    # Scheme, Turn into  Formal String
    inString  = _stream.strip(&quot;\r\n&quot;)
    # handle String
    outString = handle(inString)
    # send to port as Standart OUT
    sys.stdout.write(&quot;%s\n&quot; % (outString,))
    sys.exit(0)</pre>
<p><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2009/11/oscar-meyer-wienermobile-150x150.jpg" alt="编程语言汽车" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1839.html" class="wp_rp_title">编程语言汽车</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/11265.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2014/03/snake-hat-new-year-schedule-800x960-150x150.jpg" alt="Python修饰器的函数式编程" width="150" height="150" /></a><a href="https://coolshell.cn/articles/11265.html" class="wp_rp_title">Python修饰器的函数式编程</a></li><li ><a href="https://coolshell.cn/articles/10822.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2013/12/yoda-lambda-150x150.png" alt="函数式编程" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10822.html" class="wp_rp_title">函数式编程</a></li><li ><a href="https://coolshell.cn/articles/10169.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/5.jpg" alt="类型的本质和函数式实现" width="150" height="150" /></a><a href="https://coolshell.cn/articles/10169.html" class="wp_rp_title">类型的本质和函数式实现</a></li><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1313.html">Erlang和Python互通</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1313.html/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
