抄袭自http://www.erlangsir.com/2011/04/14/python-%E5%92%8Cerlang%E4%BA%92%E9%80%9A%E4%BE%8B%E5%AD%90/

town.erl

-module(town).
-behaviour(gen_server). -export([start/, combine/]).
-export([init/, handle_call/, handle_cast/, handle_info/, terminate/,
code_change/]).
-record(state, {port}). start() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). stop() ->
gen_server:cast(?MODULE, stop). init([]) ->
process_flag(trap_exit, true),
Port = open_port({spawn,
"python -u ./town.py"},
[stream, {line, }]
), {ok, #state{port = Port}}. handle_call({combine, Str}, _From, #state{port = Port} = State) ->
io:format("here~n"),
port_command(Port, Str), receive
{Port, {data, {_Flag, Data}}} ->
io:format("receiving:~p~n", [Data]),
timer:sleep(),
{reply, Data, Port}
end. handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(_Msg, State) ->
{noreply, State}. handle_info(Info, State) ->
{noreply, State}. terminate(_Reason, Port) ->
ok. code_change(_OldVsn, State, _Extra) ->
{ok, State}. % Internal
combine(_String) ->
start(),
Str = list_to_binary("combine|" ++ _String ++ "\n"),
gen_server:call(?MODULE, {combine, Str}).

town.py

#! /usr/bin/python
# Filename : town.py import sys def handle(_string):
if _string.startswith("combine|"):
string = "".join(_string[:].split(","))
return string """ waiting for input"""
while :
# Recv.Binary Stream as Standard IN
_stream = sys.stdin.readline() if not _stream:break
inString = _stream.strip("\r\n")
outString = handle(inString)
sys.stdout.write("%s\n" % (outString,))
sys.exit()

测试

Eshell V6.  (abort with ^G)
> town:combine("aaa+bbb").
here
receiving:"aaa+bbb"
"aaa+bbb"
>

elang和python互通的例子的更多相关文章

  1. python多线程简单例子

    python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...

  2. Python 发邮件例子

    Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...

  3. python gevent使用例子

    python gevent使用例子 from gevent.pool import Pool POOL_SIZE = 100 def process(func, param1_list, param2 ...

  4. Python random模块 例子

    最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random  random.random()用于生成一个0到1的随机符点数: ...

  5. python entry points 例子

    pbr的介绍不多,http://ju.outofmemory.cn/entry/156745 $ mkdir entry_test; cd entry_test; git init $ mkdir  ...

  6. Python练手例子(10)

    55.学习使用按位取反~. 程序分析:~0=1; ~1=0; (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) (3)将上面二者进行&运算. ...

  7. Python练手例子(4)

    16.一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. 程序分析:请参照程序Python 100例中的第14个例子 #py ...

  8. Python练手例子(3)

    13.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...

  9. Python 简单soket例子

      简单的soket例子 Python 2.0 客户端服务端传输 1.可发字符串,可发字节 bys类型 Python 3.0 客户端服务端传输 1.只能发bys,比特流的类型. 2.bys类型只能接收 ...

随机推荐

  1. centos 安装jdk/resin/mysql

    一.安装JDK 1.判断是否安装 java -version 我的计算机上使用java -version命令,内容如下: java version "1.7.0_45"OpenJD ...

  2. hdu 5777 domino 贪心

    domino Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem ...

  3. thinkphp3.2.3 定时任务重新加载, 无法加载新的定时任务的问题

    thinkphp3.2.3 的定时任务有个坑,一旦你改名定时任何或者路径,新的定时任务将无法加载,无论你重启php还是重启nginx,甚至重启服务器,都不行. 原因是你要删掉一个类似lock文件,才可 ...

  4. 使用springmvc时报错HTTP Status 400 -

    这个错误大多是因为,jsp的form表单提交的字段类型和后台接收字段类型不匹配造成的(例如,form中为String,后台接收为Integer). 我这里就是jsp表单中的日期数据没有写明类型,然后用 ...

  5. Redis 应用笔记

    模糊匹配 语法:KEYS pattern 说明:返回与指定模式相匹配的所用的keys. 该命令所支持的匹配模式如下: (1)?:用于匹配单个字符.例如,h?llo可以匹配hello.hallo和hxl ...

  6. 后台操作日志,插入数据获取的该数据主键ID为null

    代码如下: @Override public void saveTopicResource(TopicResourceModel model, Integer userId) { TopicResou ...

  7. 监控和审计IBM InfoSphere BigInsights和Cloudera Hadoop的访问权限

    http://www.ithov.com/server/124456.shtml 您也将学习一个仅适用于 IBM InfoSphere BigInsights 的快速启动监控实现. 大数据骚动主要集中 ...

  8. 【OpenGL ES】关于VBO(Vertex Buffer Object)的一些坑——解析一些关于glBuffer的函数

    最近在写毕设的时候用到OpenGL ES中的VBO,由于对一些接口用到的变量不了解被坑得很惨,在此记录一下防止以后再被坑. 本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cn ...

  9. 【Hive】数据去重

    实现数据去重有两种方式 :distinct 和 group by 1.distinct消除重复行 distinct支持单列.多列的去重方式. 单列去重的方式简明易懂,即相同值只保留1个. 多列的去重则 ...

  10. win8 商店应用 概观

    外观: 1,适应性好.可以在各种外形规格和显示大小上正确的展示数据. 2,磁贴. 交互方式: 1,应用栏.使用应用栏可以向用户显示命令和工具.默认情况下,应用栏会被隐藏,当用户使用手指从屏幕的底部边缘 ...