ERLANG和PYTHON互通實(shí)現(xiàn)過(guò)程詳解
最近開(kāi)發(fā) Erlang ,對(duì)其字符串處理能力無(wú)言至極,于是決定把它和python聯(lián)合起來(lái),打造一個(gè)強(qiáng)力的分布式系統(tǒng),等將來(lái)需要系統(tǒng)級(jí)開(kāi)發(fā)時(shí),我再把 C++/C組合進(jìn)來(lái).
首先參考了 Erlang 官方文檔和 http://blog.developers.api.sina.com.cn/?tag=erlang 以及 http://kazmier.net/computer/port-howto/ .
研讀了將近24個(gè)小時(shí), 才終于完全把問(wèn)題解決. 起名為town,town在英文里表示集市,也就是代表各種語(yǔ)言在這里的交流與互動(dòng)。) )
-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í)行時(shí),會(huì)在后臺(tái)啟動(dòng)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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python數(shù)據(jù)庫(kù)操作指南之PyMysql使用詳解
PyMySQL是在Python3.x版本中用于連接MySQL服務(wù)器的一個(gè)庫(kù),Python2 中則使用mysqldb,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)庫(kù)操作指南之PyMysql使用的相關(guān)資料,需要的朋友可以參考下2023-03-03教你如何在Pycharm中導(dǎo)入requests模塊
這篇文章主要介紹了教你如何在Pycharm中導(dǎo)入requests模塊,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09pandas中的DataFrame按指定順序輸出所有列的方法
下面小編就為大家分享一篇pandas中的DataFrame按指定順序輸出所有列的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04python實(shí)現(xiàn)數(shù)獨(dú)游戲 java簡(jiǎn)單實(shí)現(xiàn)數(shù)獨(dú)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)數(shù)獨(dú)游戲和java實(shí)現(xiàn)數(shù)獨(dú)游戲的相關(guān)代碼,比較兩種語(yǔ)言實(shí)現(xiàn)數(shù)獨(dú)游戲的區(qū)別2018-03-03使用Python玩轉(zhuǎn)串口(基于pySerial問(wèn)題)
這篇文章主要介紹了使用Python玩轉(zhuǎn)串口(基于pySerial問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python函數(shù)式編程指南(二):從函數(shù)開(kāi)始
這篇文章主要介紹了Python函數(shù)式編程指南(二):從函數(shù)開(kāi)始,本文講解了定義一個(gè)函數(shù)、使用函數(shù)賦值、閉包、作為參數(shù)等內(nèi)容,需要的朋友可以參考下2015-06-06