import os, threading, time, sys if sys.platform == 'win32': timer = time.clock else: timer = time.time def send_in_chunks(f): try: for _ in range(LENGTH // len(chunk)): f.write(chunk) finally: f.close() def recv(f, buf): try: buf.append(f.read()) finally: f.close() for i in range(10): chunk = b"X" * 4096 MB = 1024**2 LENGTH = 2**i*MB buf = [] r, w = os.pipe() r, w = os.fdopen(r, 'rb', 0), os.fdopen(w, 'wb', 0) thread = threading.Thread(target=recv, args=(r, buf)) time.sleep(1) start = timer() thread.start() send_in_chunks(w) thread.join() elapsed = timer() - start assert len(buf.pop()) == LENGTH print('amount = %.0f MB; time taken = %.3f secs; rate = %.2f MB/s' % (1.0*LENGTH/MB, elapsed, 1.0*LENGTH/MB/(elapsed+1e-9)))