抄袭自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. tcp连接的建立与释放

    1.TCP是面向连接的协议. 运输连接时用来传送TCP报文的.TCP运输连接的建立和释放是每一次面向连接的通信中必不可少的过程.因此,运输链接就有三个阶段,即:连接建立.数据传送和连接释放. 在TCP ...

  2. 深入理解Lambda

    概述 Lambda是一个表达式,也可以说它是一个匿名函数.然而在使用它或是阅读Lambda代码的时候,却显得并不那么容易.因为它匿名,因为它删减了一些必要的说明信息(比如方法名).下面就来说说Lamb ...

  3. Android TextView 设置滚动条(纯xml)

    <ScrollView android:id="@+is/scrollView_id" android:layout_width="fill_parent" ...

  4. mysql数据库(三):查询的其他用法

    一. 查询—IN的用法 语法:select ... from 表名 where 字段 a in (值b, 值c, 值d...) 等价于 select ... from 表名 where 字段a=值b ...

  5. 在阿里云上安装python3.4和pycharm

    一. 安装python3.4 二. 安装pycharm 三. 安装可视化界面和远程桌面连接 四. 启动和配置pycharm 五. 安装更多字体 六. 给pycharm设置桌面快捷方式 一. 安装pyt ...

  6. 51nod 1287 线段树

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1287 简单的线段树题目,直接写个二分查找大于等于x的最小位置就好了. # ...

  7. neutron二层网络实现

    一.基本概念 1.tap设备 比如vnet0,是虚拟化技术和KVM和XEN用来实现虚拟网卡的,当一个以太网帧发送给TAP设备时,这个以太网帧就会被虚拟机操作系统所接手,命名空间用于隔离虚拟网络设备. ...

  8. Cassandra二级索引原理——新创建了一张表格,同时将原始表格之中的索引字段作为新索引表的Primary Key,并且存储的值为原始数据的Primary Key,然后再通过pk一级索引找到真正的值

    1.什么是二级索引? 我们前面已经介绍过Cassandra之中有各种Key,比如Primary Key, Cluster Key 等等.如果您对这部分概念并不熟悉,可以参考之前的文章: [Cassan ...

  9. softmax回归(理论部分解释)

    前面我们已经说了logistic回归,训练样本是,(且这里的是d维,下面模型公式的x是d+1维,其中多出来的一维是截距横为1,这里的y=±1也可以写成其他的值,这个无所谓不影响模型,只要是两类问题就可 ...

  10. Tiny210用户手册笔记

                     核心板 CPU 处理器:  Samsung S5PV210,基于 CortexTM-A8,运行主频 1GHz                       内置 P ...