日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ERLANG和PYTHON互通實(shí)現(xiàn)過程詳解

系統(tǒng) 1684 0

最近開發(fā) Erlang ,對其字符串處理能力無言至極,于是決定把它和python聯(lián)合起來,打造一個強(qiáng)力的分布式系統(tǒng),等將來需要系統(tǒng)級開發(fā)時,我再把 C++/C組合進(jìn)來.

首先參考了 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的腳本 當(dāng)erlang中town:combine(“sentence1+sentence2”)執(zhí)行時,會在后臺啟動python的腳本,處理完畢后返回給Erlang結(jié)果: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)
          

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 乐业县| 长子县| 工布江达县| 沙湾县| SHOW| 准格尔旗| 南通市| 集贤县| 景洪市| 昌乐县| 西乌珠穆沁旗| 德江县| 林周县| 大埔区| 资兴市| 平阳县| 云安县| 和平县| 汕头市| 定安县| 三穗县| 南江县| 永吉县| 舟山市| 宕昌县| 灌阳县| 饶平县| 湄潭县| 蕉岭县| 安西县| 靖远县| 镇沅| 德昌县| 普宁市| 肥城市| 腾冲县| 揭西县| 黄大仙区| 阳春市| 元江| 南乐县|