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类型只能接收 ...
随机推荐
- c语言字符处理函数常见使用集合
1.最近看一些开源项目代码时,总会看到 c 语言中一些 "str" 开头的处理字符串的用法,有的之前没用到过,特此记录,随时看到随时添加. 这里不提出源码,只是一些使用说明加例子 ...
- StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
public int strStr(String haystack, String needle) { if(haystack == null || needle == null) { return ...
- scala学习手记35 - 隐式类型转换
先来看一下下面的内容: 2 days "ago" 5 days "from_now" 如上的内容具体应该是什么呢?不过怎么看也不像是代码.不过既然是在学代码,拿 ...
- docker之DockerSwarm的了解
这次一起了解下docker Swarm,什么是dockerSwarm. 什么是docker Swarm 产品背景 使用docker的流程,ssh到一台服务器,运行docker命令来运行本机的docke ...
- ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐
环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...
- angularjs地址栏传参
1:路由定义参数 2.controller 3. 4.目标得到参数值
- Selenium with Python 010 - unittest 框架(又称PyUnit 框架)
unittest进行python代码单元测试 calculator.py--被测试类 #!/usr/bin/env python # -*- coding: utf-8 -*- # 将要被测试的类 c ...
- Ajax基础(一)--创建请求
浏览器创建ajax请求: 1.如果是Firefox.Opera.Safari等高级浏览器,可使用XMLHttpRequest()方式创建一个新的ajax请求对象. 如果是IE浏览器,则使用Active ...
- Linux:配置Linux网络和克隆虚拟机并更改配置
Linux学习笔记1:配置Linux网络和克隆虚拟机并更改配置 一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候 ...
- java环信服务端注册IM代码
下载环信api代码 https://github.com/easemob/emchat-server-examples 里面包含各种语言版本,我只下载了java版emchat-server-java ...