|
| 1 | +"""Experiment of asynchronous os.pipe()/os.read()/os.write() |
| 2 | +
|
| 3 | +3 threads, 2 pipes, 4 i/o block sizes: |
| 4 | +
|
| 5 | +1. feeder (tx to processor at rate x) |
| 6 | +2. processor (rx from feeder at rate x1 and tx to reader at rate y) |
| 7 | +3. reader (rx from processor at rate y1) |
| 8 | +
|
| 9 | +only reader rx is non-blocking |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from ffmpegio._utils import pipe_nonblock |
| 14 | +import os |
| 15 | +import threading |
| 16 | +import time |
| 17 | +import math |
| 18 | + |
| 19 | + |
| 20 | +def processor(stdin, stdout, nin, nout, nblks): |
| 21 | + print("[processor] starting") |
| 22 | + nintotal = 0 |
| 23 | + noutottal = 0 |
| 24 | + for i in range(nblks): |
| 25 | + print(f"[processor n={i}]") |
| 26 | + nread = 0 |
| 27 | + while nread < nin: |
| 28 | + indata = os.read(stdin, nin - nread) |
| 29 | + nread += len(indata) |
| 30 | + if nread != nin: |
| 31 | + print(f"[processor] received {nread} expecting {nin}") |
| 32 | + time.sleep(1e-3) |
| 33 | + |
| 34 | + os.write(stdout, b"0" * nout) |
| 35 | + |
| 36 | + nintotal += len(indata) |
| 37 | + noutottal += nout |
| 38 | + print( |
| 39 | + f"[processor n={i}] rx {nintotal}/{nin*nblks}, tx {noutottal}/{nout*nblks}" |
| 40 | + ) |
| 41 | + |
| 42 | + os.close(stdin) |
| 43 | + os.close(stdout) |
| 44 | + print("[processor] exiting") |
| 45 | + |
| 46 | + |
| 47 | +def feeder(stdin, min, inblks): |
| 48 | + print("[feeder] starting") |
| 49 | + ntotal = 0 |
| 50 | + for i in range(inblks): |
| 51 | + print(f"[feeder {i}]") |
| 52 | + os.write(stdin, b"0" * min) |
| 53 | + ntotal += min |
| 54 | + print(f"[feeder {i}] ntotal tx'ed = {ntotal}/{min*inblks}") |
| 55 | + os.close(stdin) |
| 56 | + print("[feeder] exiting") |
| 57 | + |
| 58 | + |
| 59 | +def reader(stdout, mout, outblks, timeout): |
| 60 | + print("[reader] starting") |
| 61 | + ntotal = 0 |
| 62 | + for o in range(outblks): |
| 63 | + print(f"[reader {o}]") |
| 64 | + |
| 65 | + nread = 0 |
| 66 | + while nread < mout: |
| 67 | + try: |
| 68 | + data = pipe_nonblock.read(stdout, mout - nread) |
| 69 | + nread += len(data) |
| 70 | + # print(f"[reader {o}] rx'ed {nread}/{mout} bytes") |
| 71 | + except pipe_nonblock.NoData: |
| 72 | + # print(f"[reader {o}] no data") |
| 73 | + time.sleep(timeout) |
| 74 | + if nread > mout: |
| 75 | + raise RuntimeError(f"rx'ed too many: {nread}/{mout}") |
| 76 | + ntotal += nread |
| 77 | + print(f"[reader {o}] ntotal = {ntotal}/{mout*outblks}") |
| 78 | + |
| 79 | + os.close(stdout) |
| 80 | + print("[reader] exiting") |
| 81 | + |
| 82 | + |
| 83 | +txout, txin = os.pipe() |
| 84 | +rxout, rxin = pipe_nonblock.pipe() |
| 85 | + |
| 86 | +nblks = 3 |
| 87 | +inblks = 5 |
| 88 | +outblks = 7 |
| 89 | +g = 8 |
| 90 | + |
| 91 | +blk = g * nblks * inblks * outblks // math.gcd(nblks, inblks, outblks, g) |
| 92 | + |
| 93 | +nin = nblks * blk |
| 94 | +nout = g * nblks * blk |
| 95 | +timeout = 10e-3 |
| 96 | + |
| 97 | + |
| 98 | +proc = threading.Thread( |
| 99 | + target=processor, args=(txout, rxin, nin // nblks, nout // nblks, nblks) |
| 100 | +) |
| 101 | +proc.start() |
| 102 | + |
| 103 | +feed = threading.Thread(target=feeder, args=(txin, nin // inblks, inblks)) |
| 104 | +feed.start() |
| 105 | + |
| 106 | +read = threading.Thread(target=reader, args=(rxout, nout // outblks, outblks, timeout)) |
| 107 | +read.start() |
| 108 | + |
| 109 | + |
| 110 | +proc.join() |
| 111 | +feed.join() |
| 112 | +read.join() |
0 commit comments