2010-06-23 11:18:41
涉及的技术内容包括:ASM、ANSI C、.NET/C#、Python、Linux/Ubuntu 等。
记录读书笔记、生活点滴。关注基础理论和开发……
除特别标注外,所有文章均为作者原创,转载请注明出处。谢谢!

  查看 "推荐文章目录"。  


  研发中心长期招聘开发人员,详情请点击 "Linux/C, Python 职位"、 ".NET/C# 职位"。
  雨痕内部技术交流群,欢迎熟识的朋友参与。 将 添加为您的 MSN 联系人即可。
  为杜绝垃圾广告的侵扰,只有注册用户可以发表评论和留言。
  如果有什么问题,可以单击右侧图标给雨痕发消息。( 绿色: 在线; 灰色: 脱机)
浏览模式: 普通 | 列表
6月, 2010 | 1 | 2 | 3 | >

Keepalived 双机热备

[ 2010-06-30 20:37:34 | 作者: yuhen ]
使用 Keepalived 做双机热备非常简单,经常和 LVS 搭配来实现高可用负载平衡方案。

1. Master / Slave

首先准备两台测试服务器和一个虚拟IP。
Server A: 192.168.1.10 (主服务器)
Server B: 192.168.1.20
Virtual IP: 192.168.1.100

测试服务: 在两台服务器上分别安装 Nginx,并修改默认的 index.html 文件,显示当前服务器 IP 以便识别。

1. 在两台服务器上分别安装 keepalived。
$ sudo apt-get install keepalived
...

阅读全文…

Python Library: itertools

[ 2010-06-25 15:53:26 | 作者: yuhen ]
Python 天生就是为懒人准备的,除了语言本身的便捷外,各种类库也只有想不到而没有找不到的。itertools 提供了大量的 "迭代器" 操作手段,有点眼花缭乱。

1. 重复

count 用于创建一个可指定初始值的无限循环计数器;cycle 无限重复一个既有的迭代器;repeat 用于无限或有限重复单个对象。
In [1]: from itertools import *

In [3]: it = count(2)

In [4]: [it.next() for i in range(10)]
Out[4]: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

... ...

In [5]: it = cycle("abc")

In [6]: [it.next() for i in range(10)]
...

阅读全文…

Python Library: heapq

[ 2010-06-23 21:35:58 | 作者: yuhen ]
最小堆: 完全平衡二叉树,所有节点都小于其子节点。

堆的意义:最快找到最大/最小值。在堆结构中插入或删除最小(最大)元素时进行重新构造时间复杂度为O(logN),而其他方法最少为O(N)。堆在实际开发中的更倾向于算法调度而非排序。比如优先级调度时,每次取优先级最高的;时间驱动调度时,取时间最小或等待最长的等等。
In [1]: from heapq import *

In [2]: from random import *

In [3]: rand = sample(range(10), 10) # 生成随机整数列表

In [4]: rand
Out[4]: [9, 2, 8, 5, 3, 6, 4, 1, 0, 7]

In [5]: heap = []

In [6]: for i in rand: heappush(heap, i) # 将随机数压入堆
...

阅读全文…

Python Library: random

[ 2010-06-23 20:47:34 | 作者: yuhen ]
伪随机数生成模块。如果不提供 seed,默认使用系统时间。

使用相同的 seed,可以获得完全相同的随机数序列,常用于算法改进测试。
In [1]: from random import *

In [2]: a = Random(); a.seed(1)

In [3]: [a.randint(1, 100) for i in range(20)]
Out[3]: [14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 44, 77, 1, 45, 73, 23, 95, 91, 4]

In [4]: b = Random(); b.seed(1)

In [5]: [b.randint(1, 100) for i in range(20)]
Out[5]: [14, 85, 77, 26, 50, 45, 66, 79, 
...

阅读全文…

Python Library: multiprocessing (4)

[ 2010-06-23 16:09:47 | 作者: yuhen ]
4. Pool

创建多个工作进程(work process)来完成类似线程池的工作。
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os, time, sys, atexit
from multiprocessing import *

def test(i):
    print current_process().pid, i
    time.sleep(0.01)
    return i * 2

if __name__ == "__main__":

    pool = Pool(3)

    for i in range(5):
        # 处理单个任务
        ret = pool.apply(test, [i])
        print "apply return:", ret

    # 处理多个任务
    pool.map(test, range(10))
...

阅读全文…

Python Library: multiprocessing (3)

[ 2010-06-22 22:54:16 | 作者: yuhen ]
3. Sharing state

很多时候,我们需要在父子进程间保持某种状态控制,还可能需要同耕一块 "水浇地"。

(1) Shared memory

multiprocess.Value 和 Array 都是通过调用 mmap 创建描述符为 -1 的匿名存储映射来实现共享存储区的。
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os, time, sys, atexit
from multiprocessing import *

def test(event, value, array):
    event.wait()
    print "child:", value.value, array.value

    value.value = 200
    array.value = "Hi!"
...

阅读全文…

Python Library: multiprocessing (2)

[ 2010-06-22 13:41:16 | 作者: yuhen ]
2. Exchanging objects

传递数据(对象)和共享状态是有区别的,前者会将对象序列化后交给子进程,从此各不相干。(序列化内容请参考 《Python Library: Serialize》)

(1) Pipe

利用 Pipe 在进程间通讯是 Linux 编程最常用的手法。
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os, time
from multiprocessing import *

def test(conn):
    print conn.recv()
    conn.send("child: Pipe!")

if __name__ == "__main__":
...

阅读全文…

Python Library: multiprocessing (1)

[ 2010-06-20 12:35:45 | 作者: yuhen ]
Python Standard Library 从 2.6 起增加了子进程级别的并行开发支持 —— multiprocessing。
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os, time
from multiprocessing import *

def test(x):
    print current_process().pid, x
    time.sleep(1)

if __name__ == "__main__":
    print "main:", os.getpid()
    p = Pool(5)
    p.map(test, range(13))

输出:
$ ./test.py

main: 1208
1209 0
1210 1
1211 2
...

阅读全文…
1 | 2 | 3 | >