Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1666121
  • 博文数量: 607
  • 博客积分: 10031
  • 博客等级: 上将
  • 技术积分: 6633
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-30 17:41
文章分类

全部博文(607)

文章存档

2011年(2)

2010年(15)

2009年(58)

2008年(172)

2007年(211)

2006年(149)

我的朋友

分类:

2009-10-07 23:42:48

第一出处 http://pascal4123.javaeye.com/admin/blogs/434927

2009-07-28

erlang-python port 程序

作者:pascal4123

书上第12章那个erlang-c的例子改编了下,把C程序改写成了python程序。在ubuntu8.04 + erlang5.6.5 + python2.5.2上调试通过。

erlang side: example2.erl

Erlang代码
  1. -module(example2).  
  2. -export([start/0, stop/0]).  
  3. -export([twice/1, sum/2]).  
  4.   
  5. start() ->  
  6.     spawn(fun() ->  
  7.           register(example1, self()),  
  8.           process_flag(trap_exit, true),  
  9.           Port = open_port({spawn, "python -u ./example2.py"}, [{packet, 2}]),  
  10.           loop(Port)  
  11.       end).  
  12.   
  13. stop() ->  
  14.     example1 ! stop.  
  15.   
  16. twice(X) -> call_port({twice, X}).  
  17. sum(X,Y) -> call_port({sum, X, Y}).  
  18.   
  19. call_port(Msg) ->  
  20.     example1 ! {call, self(), Msg},  
  21.     receive  
  22.     {example1, Result} ->  
  23.         Result  
  24.     end.  
  25.   
  26. loop(Port) ->  
  27.     receive  
  28.     {call, Caller, Msg} ->  
  29.         Port ! {self(), {command, encode(Msg)}},   
  30.         receive  
  31.         {Port, {data, Data}} ->  
  32.             Caller ! {example1, decode(Data)}  
  33.         end,  
  34.         loop(Port);  
  35.     stop ->  
  36.         Port ! {self(), close},  
  37.         receive  
  38.         {Port, closed} ->  
  39.             exit(normal)  
  40.         end;  
  41.     {'EXIT', Port, Reason} ->  
  42.         exit({port_terminated,Reason})  
  43.     end.  
  44.   
  45. encode({twice, X}) -> [1, X];    
  46. encode({sum, X, Y}) -> [2, X, Y].   
  47.   
  48. decode([Int]) -> Int.   

需要注意的是调用python程序,要使用参数-u。强制标准输入/输出不得有缓存,要立时反应。
python官方文档对-u的解释: Force stdin, stdout and stderr to be totally unbuffered.


python side: example2.py

Python代码
  1. import sys, os  
  2. import struct  
  3.   
  4. def recv():  
  5.     buf = sys.stdin.read(2)  
  6.     if len(buf) == 2:  
  7.         (sz, ) = struct.unpack("!h", buf)  
  8.         payload = sys.stdin.read(sz)  
  9.         return payload  
  10.     else:  
  11.         return None  
  12.   
  13. def send(payload):  
  14.     sz = len(payload)  
  15.     header= struct.pack("!h", sz)  
  16.     return sys.stdout.write( header + payload )  
  17.   
  18. def twice(arg1):  
  19.     return 2*arg1  
  20.   
  21. def sum(arg1,arg2):  
  22.     return arg1+arg2  
  23.   
  24. def main_loop():  
  25.     buf = recv()  
  26.     while buf:  
  27.         xa = struct.unpack("!%dB" % len(buf), buf)  
  28.         if xa[0] == 1:  
  29.             calc_res = twice(xa[1])  
  30.         elif xa[0] == 2:  
  31.             calc_res = sum(xa[1], xa[2])  
  32.         result = struct.pack('!B', calc_res)  
  33.         send(result)  
  34.         buf = recv()  
  35.     return None  
  36.   
  37. if __name__ == '__main__':  
  38.     main_loop() 

阅读(1207) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~