# Py 2 Erl

### Zom.Quiet

# 文档版本

- 版本历史::
  - v0.9.2 071013 完善思考路径，补充刚刚了解的技术理解
  - v0.9.1 071012 补充诘问论述
  - v0.9 071011 发现新内容！
  - v0.8 071010 完善主体内容
  - v0.7 071009 创建,调节模板CSS

#

## 我是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
热部署!

# 自问

- Py 和 Erl 都N强大
- 但是结合?!:
  - 要如何辨别合适的情景?
  - 要如何处理互眹互通?
  - 要如何发挥双方特点?
  - 要如何....

#

## Python
擅长...

#

## I/O

#

## 事务逻辑

#

## Erlang
特长...

#

## Py,Erl
互补场景

#

## 要求容错

#

## 高度
可扩展

#

## 嗯嗯...

#

## 游戏!
网络的

#

## 短信增值业务

#

## SocialNet
Service

#

## Python
主外

#

## Erlang
主内

#

## 融合
何如?!

# Thanx ... 提问时间

- Zoom.Quiet ＠ gmail.com
- - 一切资料来自互联网
  - 所有想法来自日常学习工作
  - 所有体悟来自各种沟通分享
  - 所有知识来自网络分享印证
  - 所有经验来自个人失败经历……

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

# 参考

- 网络文章::
  - Python如何获得Erlang的威力?
  - Erlang in Python
  - 用Erlang实现一个简单的Web服务器die
  - 泼点Erlang的冷水
  - Python中的Erlang－Candygram
  - ...

# 关于...

- 幻灯展示:
  - S5 : A Simple Standards-Based Slide Show System
  - 纯HTML 幻灯效果工具 本地介绍页面
- 编写环境:
  - Leo :
    文学编程环境
  - 纯Python 编写的通用编辑环境