2011年11月9日 星期三

[Python] property decorator

正常用法是http://docs.python.org/library/functions.html#property

以下是鬼打牆用法...

應該把Dgram裡面的started改名字才對~

似乎在舊得python可以work


class Base(object):
a = 1
def __init__(self, listener, handle=None, backlog=None, spawn='default'):
self._xxx = 'xxx'
# the faulure is because of started name is used by derived object Dram
self.started = None

class Dgram(Base):
b = 2
def __init__(self, listener, handle=None, backlog=None, spawn='default', **args):
Base.__init__(self, listener, handle=handle, backlog=backlog, spawn=spawn)
self.delay = 123

@property
def started(self):
return self._recv_event is not None or self._start_receving_timer is not None

def echo(msg, addr):
print 'echo ' + msg

if __name__ == '__main__':
s = Dgram('123', echo)

ytshen@ytshen-ThinkCentre-A58:~/temp$ python test_obj.py
Traceback (most recent call last):
File "test_obj.py", line 23, in
s = Dgram('123', echo)
File "test_obj.py", line 12, in __init__
Base.__init__(self, listener, handle=handle, backlog=backlog, spawn=spawn)
File "test_obj.py", line 6, in __init__
self.started = None
AttributeError: can't set attribute

2011年11月2日 星期三

[RabbitMQ] priority queue

目前RabbitMQ沒有直接支援priority queue的功能, 所以目前只能work around, 可以在client動點手腳

建立logical queue, 並根據priority的數量, 來建立physical queue (ex: 如果只有high & low priority, 那就建立兩個queue)

方法一
client polling, 按照priority從相對應的physical queue取出message

方法二
client subsribe queue, 並且限制channel prefetch counts, 防止subscription flood, 並且在local建立in memory priority queue, 直到message被處理, 在送ack給RabbitMQ server 

Reference
http://dougbarth.github.com/2011/07/01/approximating-priority-with-rabbitmq.html 
http://dougbarth.github.com/2011/06/10/keeping-the-rabbit-on-a-leash.html