elang和python互通的例子
抄袭自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互通的例子的更多相关文章
- python多线程简单例子
python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...
- Python 发邮件例子
Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...
- python gevent使用例子
python gevent使用例子 from gevent.pool import Pool POOL_SIZE = 100 def process(func, param1_list, param2 ...
- Python random模块 例子
最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random random.random()用于生成一个0到1的随机符点数: ...
- python entry points 例子
pbr的介绍不多,http://ju.outofmemory.cn/entry/156745 $ mkdir entry_test; cd entry_test; git init $ mkdir ...
- Python练手例子(10)
55.学习使用按位取反~. 程序分析:~0=1; ~1=0; (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) (3)将上面二者进行&运算. ...
- Python练手例子(4)
16.一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. 程序分析:请参照程序Python 100例中的第14个例子 #py ...
- Python练手例子(3)
13.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- Python 简单soket例子
简单的soket例子 Python 2.0 客户端服务端传输 1.可发字符串,可发字节 bys类型 Python 3.0 客户端服务端传输 1.只能发bys,比特流的类型. 2.bys类型只能接收 ...
随机推荐
- windchill系统——开发_生命周期状态的增加
步骤如下 ResourceBuild wt.lifecycle.StateRB ant -f codebase/MakeJar.xml 这样就可以了,下面是查看
- eclipse文档字体大小设置
步骤如下
- webstorm打开带有node_modules文件夹的工程时很卡
ctrl+alt+s打开settings 在webstorm中配置这个就可以不加载出来node_modules使页面加载快
- JSP web.xml <jsp-config>标签使用详解
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2 ...
- 阻止a标签跳转四种方法 兼容各大浏览器(包括IE)
阻止a标签跳转四种方法 兼容各大浏览器(包括IE) HTML <!--第一种--> <a href="javascript:;">我不会被跳转</a& ...
- R语言和中国地图
上图是R语言绘制的按地域分布的数据图.更科学,更严谨,也更有质感的样子. 今天瞎写点东西,我在想数据分析的意义是什么,也许就是研究事物存在的形式.而事物存在的形式是什么样子呢,从最初的三维空间,爱因斯 ...
- python 实现简单点名程序
程序会遍历文件所有姓名,遍历完之前不会有重复值,遍历所有后将提示推出. #-*-coding:utf-8-*-#author:wangxing #点名程序 import randomimport os ...
- 计算从哪天起应该购买预售火车票.cs
代码直接CSC编译即可. 计算从哪天起应该购买预售火车票.cs using System; using System.Diagnostics; using System.IO; class Progr ...
- MySQL忘记密码解决方案
1.修改本地mysql目录中的my.ini文件 添加skip-grant-tables 2.在win +r 输入cmd,进行mysql的重启启动操作 net stop MySQL 停止服务 ...
- MailJobUtils 发送邮件
import java.util.List; import java.util.Properties; import javax.annotation.Resource; import javax.m ...