Py 2 Erl

Zom.Quiet

文档版本

我是Pythoner

Python
之妙

Pythonic

垂涎
Erlang

迸发响应

天然
分布式

热部署

结合!
何如?

Py<->Erl
?

困难!

OOP
vs FP

名称空间
vs消息

Erl->Py
?

Socket?

Erl Srv.

声明

-module(eserver).
-export([startd/0, start/0,start/1,process/1]).
-define(defPort,2000).
startd() ->
    register(eserverp, spawn(?MODULE, start, [])).   
start() -> start(?defPort).
start(Port) ->
    case gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]) of
        {ok, LSock}  -> server_loop(LSock);
        {error, Reason} -> exit({Port,Reason})
    end.
...

Erl Srv..

main loop

%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock) ->
    case gen_tcp:accept(LSock) of
        {ok, Sock}        ->
            spawn(?MODULE,process,[Sock]),
            server_loop(LSock);
        {error, Reason}        ->
            exit({accept,Reason})
    end.
...

Erl Srv....

连接处理

%% process current connection
process(Sock) ->
    Req = do_recv(Sock),
    Resp = "hello world",
    do_send(Sock,Resp),
    gen_tcp:close(Sock).
%% send a line of text to the socket
do_send(Sock,Msg) ->
    case gen_tcp:send(Sock, Msg) of
        ok                -> ok;
        {error, Reason}        -> exit(Reason)
    end.
...

Erl Srv.....

请求响应

%% receive data from the socket
do_recv(Sock) ->
    case gen_tcp:recv(Sock, 0) of
        {ok, Bin}        -> binary_to_list(Bin);
        {error, closed}        -> exit(closed);
        {error, Reason}        -> exit(Reason)
    end.

Py Client

简要客户端

import socket
HOST = 'localhost' # The remote host
PORT = 6889  # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

中间文件?

XML-RPC?

erst->
port?

婆烦!

Py->Erl
?

线程->
进程

勉强!

PyErlang
已死

面向线程

增强!

Stackless
Python

惊喜!

Py+Erl =
Candygram

面向消息

Candygram e.g.

import candygram as cg
class Thread:
    ...
# Create new thread.
counter = cg.spawn(Thread().run)
response = cg.Receiver()
response.addHandler((counter, int), lambda msg: msg[1], cg.Message)
# Tell thread to increment twice.
counter.send('increment')
counter.send('increment')
# Request thread's current value.
counter.send((cg.self(), 'value'))
# Print the response
print response.receive()
# Tell thread to increment one more time.
counter.send('increment')
counter.send((cg.self(), 'value'))
print response.receive()
counter.send('stop')

Py仿
Erlang

Jython

线程
(+__+)

Py2Erl
图什么

for
并发

for
分布计算

for
热部署!

自问

Python
擅长...

I/O

事务逻辑

Erlang
特长...

Py,Erl
互补场景

要求容错

高度
可扩展

嗯嗯...

游戏!
网络的

短信增值业务

SocialNet
Service

Python
主外

Erlang
主内

融合
何如?!

Thanx ... 提问时间

[any material that should appear in print but not on the slide]

参考

关于...