抄袭自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. python 获取探针页面,自动查询公司出口

    在一些渗透当中,我们需要批量探针出口来达到我们的目的. 所以就有了这个丑陋简洁的小脚本. #!/usr/bin/env python #-*- coding:utf- -*- import sys i ...

  2. NumPy来自现有数据的数组

    NumPy - 来自现有数据的数组 这一章中,我们会讨论如何从现有数据创建数组. numpy.asarray 此函数类似于numpy.array,除了它有较少的参数. 这个例程对于将 Python 序 ...

  3. scala学习手记3 - var和val

    scala中用var和val定义变量都是可以的. 用val定义的变量是不可变的,被初始化后值就固定下来,不可以再被修改(这类似于java中的final关键字):用var定义的变量是可变的,可以任意修改 ...

  4. C-RAN

    无线接入网(RAN)是移动运营商赖以生存的重要资产.传统的无线接入网具有以下特点: 1. 每一个基站连接若干个固定数量的扇区天线,并覆盖小片区域,每个基站只能处理本小区收发信号: 2. 系统的容量是干 ...

  5. 初探UiAutomator2.0中使用Xpath定位元素

    J 今天的主题是讲一下在使用过程中遇到的一个问题,如何在UiAutomator2.0中使用Xpath定位元素? 背景 现在的app在打包成apk的时候都是有加固处理的,各种混淆加固,所以已经破坏了或扰 ...

  6. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  7. 【Python】循环设计

    转载:作者:Vamei 出处:http://www.cnblogs.com/vamei range() 在Python中,for循环后的in跟随一个序列的话,循环每次使用的序列元素,而不是序列的下标. ...

  8. HTML 中 id与name 区别

    一个name可以同时对应多个控件,比如checkbox和radio而id必须是全文档中唯一的 id的用途1) id是HTML元素的Identity,主要是在客户端脚本里用.2) label与form控 ...

  9. dubbo常见报错

    1. java.io.IOException: Can not lock the registry cache file C:\Users\Administrator\.dubbo\dubbo-reg ...

  10. boost库做什么用呢?

    1.C++标准库不是已经很全面了吗?Boost又不是界面库,它主要解决些什么问题呢?哪类问题?2.Boost的开发人员都是C++标准委员会的吧,为什么没把它列做标准库,有什么不完善的问题吗? 3.Bo ...