def monitor(send, pipe, blocksize=1, daemon=True, name=None):
"""Spawn a thread that reads `blocksize` bytes from `pipe` and dispatches it to `send`
For every single byte, `send` is called. The thread is named according to
the `name` parameter.
Returns the monitoring threading.thread instance
"""
def shuffle(send, pipe):
while not pipe.closed:
data = pipe.read(blocksize)
if len(data) == 0:
# pipe.read syscall was interrupted. so since we can't really
# determine why (cause...y'know..python), stop dancing so
# the parent will actually be able to terminate us
break
map(send,data)
return
if name:
monitorThread = threading.Thread(target=shuffle, name=name, args=(send,pipe))
else:
monitorThread = threading.Thread(target=shuffle, args=(send,pipe))
monitorThread.daemon = daemon
return monitorThread
评论列表
文章目录