python stupid trick
February 18, 2018
pythonpython queue: put、get、task_done、join
Queue.put(item[, block[, timeout]])
:- Put item into the queue.
- If optional args block is true and timeout is None (the default), block if necessary until a free slot is available.
- If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time.
- Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).
Queue.get([block[, timeout]])
:- Remove and return an item from the queue.
- If optional args block is true and timeout is None (the default), block if necessary until an item is available.
- If timeout is a positive number, it **blocks at most timeout seconds and raises the Empty exception if no item was available within that time. **
- Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).
Queue.task_done()
:- Indicate that a formerly enqueued task is complete.
- Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.
- If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).
- Raises a ValueError if called more times than there were items placed in the queue.
Queue.join()
:- Blocks until all items in the queue have been gotten and processed.
- The count of unfinished tasks goes up whenever an item is added to the queue.
- The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete.
- When the count of unfinished tasks drops to zero, join() unblocks.
The KeyboardInterrupt Exception:
- If you try to stop a CPython program using Control-C, the interpreter throws a KeyboardInterrupt exception.
- this is an ordinary exception, and is, like all other exceptions, caught by a “catch-all” try-except statement
- if the update process isn’t an atomic operation in itself, it’s also a good idea to use database transactions, and roll back when something goes wrong
for record in database:
begin()
try:
process(record)
if changed:
update(record)
except (KeyboardInterrupt, SystemExit):
rollback()
raise
except:
rollback()
# report error and proceed
else:
commit()
装饰器:很方便的给不同的对象增添相同的功能
def singleton(cls):
instance = cls()
instance.__call__ = lambda: instance
return instance
@singleton
class Highlander:
x = 100
# Of course you can have any attributes or methods you like.
highlander = Highlander()
another_highlander = Highlander()
assert id(highlander) == id(another_highlander)
metaclass:希望的是有单例特性的类而不是为类添加单例限制
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls._instance = None
def __call__(cls, *args, **kw):
if cls._instance is None:
cls._instance = super(Singleton, cls).__call__(*args, **kw)
return cls._instance
class MyClass(object):
__metaclass__ = Singleton
one = MyClass()
two = MyClass()
- 检查多个waitable object并阻塞,直到它变成ready object为止,并返回相应的ready object列表
接收四个参数:
- 用于读操作的 waitable object 列表
- 用于写操作的 waitable object 列表
- 表示异常的 waitable object 列表
- 超时时间:超时后select函数返回0,并且上述三个列表为空
返回三项返回值:
- 用于读操作的 ready object 列表
- 用于写操作的 ready object 列表
- 表示异常的 ready object 列表
import socket
import select
s = socket.socket()
s.bind(('', 50007))
s.listen(5)
inputs = [s]
num = 0
while True:
rs, ws, es = select.select(inputs, [], [])
for r in rs:
if r is s:
c, addr = s.accept()
print 'got conn from :', addr
inputs.append(c)
else:
data = r.recv(1024)
r.sendall('You send: ' + data)
if not data:
inputs.remove(r)
else:
print 'receive data : ', data