代码改变世界

python中poolable queue

2017-01-24 15:58  放作夥  阅读(380)  评论(0编辑  收藏  举报

在python cookbook中看见这么一段代码
import queue
import socket
import os

class PollableQueue(queue.Queue):
def init(self):
super().init()
# Create a pair of connected sockets
if os.name == 'posix':
self._putsocket, self._getsocket = socket.socketpair()
else:
# Compatibility on non-POSIX systems
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 0))
server.listen(1)
self._putsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._putsocket.connect(server.getsockname())
self._getsocket, _ = server.accept()
server.close()

def fileno(self):
    return self._getsocket.fileno()

def put(self, item):
    super().put(item)
    self._putsocket.send(b'x')

def get(self):
    self._getsocket.recv(1)
    return super().get()

一开始不明白为什么实现了poolable,原来select函数的参数是3个列表,包含整数文件描述符,或者带有可返回文件描述符的fileno()方法对象。

原来只需要支持fileno方法就可以给select调用了。

客户端代码:
import select
import threading

def consumer(queues):
'''
Consumer that reads data on multiple queues simultaneously
'''
while True:
can_read, _, _ = select.select(queues,[],[])
for r in can_read:
item = r.get()
print('Got:', item)

q1 = PollableQueue()
q2 = PollableQueue()
q3 = PollableQueue()
t = threading.Thread(target=consumer, args=([q1,q2,q3],))
t.daemon = True
t.start()

Feed data to the queues

q1.put(1)
q2.put(10)
q3.put('hello')
q2.put(15)