ERLANG和PYTHON互通实现过程详解


Posted in Python onJuly 05, 2019

最近开发 Erlang ,对其字符串处理能力无言至极,于是决定把它和python联合起来,打造一个强力的分布式系统,等将来需要系统级开发时,我再把 C++/C组合进来.

首先参考了 Erlang 官方文档和 http://blog.developers.api.sina.com.cn/?tag=erlang 以及 http://kazmier.net/computer/port-howto/ .

研读了将近24个小时, 才终于完全把问题解决. 起名为town,town在英文里表示集市,也就是代表各种语言在这里的交流与互动。) )

-module(town).
-behaviour(gen_server).
 
%% API
-export([start/0,combine/1]).
 
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {port}).
 
start() ->
 gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
stop() ->
 gen_server:cast(?SERVER, stop).
init([]) ->
 process_flag(trap_exit, true),
 Port = open_port({spawn, "python -u /home/freefis/Desktop/town.py"},[stream,{line, 1024}]),
 {ok, #state{port = Port}}.
 
handle_call({combine,String}, _From, #state{port = Port} = State) ->
 port_command(Port,String),
 receive
 {Port,{data,{_Flag,Data}}} ->
  io:format("receiving:~p~n",[Data]),
  sleep(2000),
  {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(),
 String = list_to_binary("combine|"++_String++"\n"),
 gen_server:call(?SERVER,{combine,String},infinity),
 stop().

这段是Python的脚本 当erlang中town:combine(“sentence1+sentence2”)执行时,会在后台启动python的脚本,处理完毕后返回给Erlang结果:sentence1sentence2,然后退出。

import sys
def handle(_string):
 if _string.startswith("combine|"):
  string = "".join( _string[8:].split(","))
  return string
 
"""waiting for input """
while 1:
 # Recv. Binary Stream as Standard IN
 _stream = sys.stdin.readline()
 
if not _stream: break
 # Scheme, Turn into Formal String
 inString = _stream.strip("\r\n")
 # handle String
 outString = handle(inString)
 # send to port as Standart OUT
 sys.stdout.write("%s\n" % (outString,))
 sys.exit(0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中replace方法实例分析
Aug 20 Python
Python中第三方库Requests库的高级用法详解
Mar 12 Python
Python编程修改MP3文件名称的方法
Apr 19 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
python实现QQ批量登录功能
Jun 19 Python
python 并发编程 阻塞IO模型原理解析
Aug 20 Python
Python面向对象程序设计之静态方法、类方法、属性方法原理与用法分析
Mar 23 Python
python如何判断IP地址合法性
Apr 05 Python
在echarts中图例legend和坐标系grid实现左右布局实例
May 16 Python
Python TestSuite生成测试报告过程解析
Jul 23 Python
利用python 下载bilibili视频
Nov 13 Python
pytorch 权重weight 与 梯度grad 可视化操作
Jun 05 Python
python如何读取bin文件并下发串口
Jul 05 #Python
anaconda如何查看并管理python环境
Jul 05 #Python
python笔记之mean()函数实现求取均值的功能代码
Jul 05 #Python
python如何给字典的键对应的值为字典项的字典赋值
Jul 05 #Python
python调用并链接MATLAB脚本详解
Jul 05 #Python
python实现最大子序和(分治+动态规划)
Jul 05 #Python
Python实现最大子序和的方法示例
Jul 05 #Python
You might like
zf框架的session会话周期及次数限制使用示例
2014/03/13 PHP
php设计模式之职责链模式实例分析【星际争霸游戏案例】
2020/03/27 PHP
javascript 面向对象编程 聊聊对象的事
2009/09/17 Javascript
Javascript的一种模块模式
2010/09/08 Javascript
JQuery获取浏览器窗口内容部分高度的代码
2012/02/24 Javascript
js的touch事件的实际引用
2014/10/13 Javascript
JQuery中使用on方法绑定hover事件实例
2014/12/09 Javascript
jQuery实现的产品自动360度旋转展示特效源码分享
2015/08/21 Javascript
浅谈angular.js中实现双向绑定的方法$watch $digest $apply
2015/10/14 Javascript
五种js判断是否为整数类型方式
2015/12/03 Javascript
使用JS读取XML文件的方法
2016/11/25 Javascript
AngularJS控制器controller给模型数据赋初始值的方法
2017/01/04 Javascript
vue实现设置载入动画和初始化页面动画效果
2019/10/28 Javascript
[48:56]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 VG vs KG
2018/03/31 DOTA
学习python (1)
2006/10/31 Python
用python写个自动SSH登录远程服务器的小工具(实例)
2017/06/17 Python
Python实现针对给定单链表删除指定节点的方法
2018/04/12 Python
python调用支付宝支付接口流程
2019/08/15 Python
深入浅析Python科学计算库Scipy及安装步骤
2019/10/12 Python
Tensorflow: 从checkpoint文件中读取tensor方式
2020/02/10 Python
浅谈selenium如何应对网页内容需要鼠标滚动加载的问题
2020/03/14 Python
Python HTTP下载文件并显示下载进度条功能的实现
2020/04/02 Python
pandas之分组groupby()的使用整理与总结
2020/06/18 Python
Selenium及python实现滚动操作多种方法
2020/07/21 Python
Pycharm中使用git进行合作开发的教程详解
2020/11/17 Python
PyCharm2020.3.2安装超详细教程
2021/02/08 Python
美国领先的医疗警报服务:Philips Lifeline
2018/03/12 全球购物
中学教师请假制度
2014/02/03 职场文书
土木工程求职信
2014/05/29 职场文书
班子成员四风问题自我剖析材料
2014/09/29 职场文书
2014年财务经理工作总结
2014/12/08 职场文书
结婚保证书
2015/01/16 职场文书
客房领班岗位职责
2015/02/11 职场文书
2016中秋节晚会开场白
2015/11/26 职场文书
2016年小学生教师节广播稿
2015/12/18 职场文书
vue 自定义的组件绑定点击事件
2022/04/21 Vue.js