#!/usr/bin/env python # Provide copies of fifos with fixed-size buffers, and periodically print a # report of how much space has been used. I wrote this to try and improve # mythtranscode's performance a bit, but it didn't turn out to be useful -- it # should have other applications, though. import sys, os, select, errno, time def main(args): fifos = [] limit = {} bufs = {} used = {} ifds = {} ifds_b = {} ofds = {} ofds_b = {} for i in range(0, len(args), 2): f = args[i + 1] fifos.append(f) limit[f] = 1024 * 1024 * int(args[i]) bufs[f] = [] used[f] = 0 oname = f + ".buf" ifds[f] = os.open(f, os.O_RDONLY | os.O_NONBLOCK) ifds_b[ifds[f]] = f try: os.unlink(oname) except OSError: pass os.mkfifo(oname) ofds[f] = os.open(oname, os.O_RDWR | os.O_NONBLOCK) ofds_b[ofds[f]] = f last = time.time() while fifos != []: rs = [ifds[f] for f in fifos if used[f] < limit[f]] ws = [ofds[f] for f in fifos if bufs[f] != []] (rs, ws, xs) = select.select(rs, ws, [], 1.0) now = time.time() if (now - last) > 10: last = now print "Buffer usage:" for f in fifos: print " %-20s %d" % (f, used[f]) for fd in rs: f = ifds_b[fd] try: data = os.read(fd, 65536) except OSError, e: if e.errno == errno.EWOULDBLOCK: continue bufs[f].append(data) used[f] += len(data) for fd in ws: f = ofds_b[fd] buf = bufs[f] if buf[0] == "": os.close(ifds[f]) os.close(ofds[f]) fifos.remove(f) else: try: count = os.write(fd, buf[0]) except OSError, e: if e.errno == errno.EWOULDBLOCK: continue if count == len(buf[0]): buf.pop(0) else: buf[0] = buf[0][count:] used[f] -= count if __name__ == "__main__": main(sys.argv[1:])