def RunWorker(sock_in, sock_out, max_size): #f_in = sock_in.makefile('r+') f_out = sock_out.makefile('r+') byte_count = 0 line_count = 0 min_size = max(max_size - 1000, 1) expected_line_count = max_size - min_size + 1 expected_byte_count = (max_size + min_size) * (max_size - min_size + 1) / 2 for i in xrange(min_size, max_size + 1): data = '*' * (i - 1) + '\n' f_out.write(data) f_out.flush() while sock_in and coio.select([sock_in], (), (), 0)[0]: data = sock_in.recv(8192) if data: byte_count += len(data) line_count += data.count('\n') else: sock_in = None sock_out.shutdown(1) # Shut down for writing. if sock_in: # Read the rest. while True: data = sock_in.recv(8192) if not data: break byte_count += len(data) line_count += data.count('\n') assert byte_count == expected_byte_count assert line_count == expected_line_count
#! /usr/local/bin/stackless2.6 # by [email protected] at Mon Apr 19 02:02:18 CEST 2010 """Demo code for select(2) with Syncless. Please note that select(2) is inherently slow. Please see FAQ entry Q14 in README.txt . """ import sys from syncless import coio if __name__ == '__main__': if len(sys.argv) <= 1 or sys.argv[1] == 'a': print >> sys.stderr, 'A' # Returns, immediately, retval[1] becomes [sys.stderr, sys.stdout] or # [sys.stdout, sys.stderr]. print >> sys.stderr, coio.select([sys.stdin], [sys.stderr, sys.stdout], [], 3) elif len(sys.argv) > 1 and sys.argv[1] == 'b': print >> sys.stderr, 'B' # Times out after 3 seconds. print >> sys.stderr, coio.select([sys.stdin], [], [], 3) elif len(sys.argv) > 1 and sys.argv[1] == 'c': print >> sys.stderr, 'C' # Waits indefinitely. print >> sys.stderr, coio.select([sys.stdin], [], [], None) else: assert 0
#! /usr/local/bin/stackless2.6 # by [email protected] at Mon Apr 19 02:02:18 CEST 2010 """Demo code for select(2) with Syncless. Please note that select(2) is inherently slow. Please see FAQ entry Q14 in README.txt . """ import sys from syncless import coio if __name__ == "__main__": if len(sys.argv) <= 1 or sys.argv[1] == "a": print >>sys.stderr, "A" # Returns, immediately, retval[1] becomes [sys.stderr, sys.stdout] or # [sys.stdout, sys.stderr]. print >>sys.stderr, coio.select([sys.stdin], [sys.stderr, sys.stdout], [], 3) elif len(sys.argv) > 1 and sys.argv[1] == "b": print >>sys.stderr, "B" # Times out after 3 seconds. print >>sys.stderr, coio.select([sys.stdin], [], [], 3) elif len(sys.argv) > 1 and sys.argv[1] == "c": print >>sys.stderr, "C" # Waits indefinitely. print >>sys.stderr, coio.select([sys.stdin], [], [], None) else: assert 0