def setUp(self): import socket sock1, sock2 = coio.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) self.sock1 = sock1 self.f = coio.nbfile(sock1.fileno(), sock2.fileno(), write_buffer_limit=0, do_close=0, close_ref=(sock1, sock2))
def setUp(self): self.assertEqual(2, coio.stackless.getruncount()) try: read_fd, write_fd = os.pipe() self.f = coio.nbfile(read_fd, write_fd, write_buffer_limit=0, do_close=1) read_fd = None finally: if read_fd is not None: # Construction failed. os.close(read_fd) os.close(write_fd)
def testFairSchedulingBlockedOnFile(self): events = [] def Worker(name, count): while count > 0: events.append(name) count -= 1 if count > 0: stackless.schedule() nbf = coio.nbfile(*os.pipe()) try: def SenderWorker(name, count): while count > 0: events.append(name) count -= 1 if count > 0: stackless.schedule() events.append('R') nbf.write('S') nbf.flush() events.append('T') def ReceiverWorker(name): events.append(name) nbf.read_at_most(1) events.append(name.lower()) stackless.tasklet(SenderWorker)('A', 3) stackless.tasklet(Worker)('B', 6) stackless.tasklet(ReceiverWorker)('W') stackless.tasklet(Worker)('C', 9) for i in xrange(32): stackless.schedule() self.assertEqual( 'ABWC' # First iteration, in tasklet creation order. 'ABC' # W is blocked on reading now. 'ARTBC' # A sends 'S' to wake up W. 'wBC' # W woken up, inserted to the beginning of the chain. 'BC' 'BC' 'C' # B's counter has expired. 'C' 'C', ''.join(events)) nbf.close() finally: nbf.close()
def testFairSchedulingWithFile(self): events = [] def Worker(name, count): while count > 0: events.append(name) stackless.schedule() count -= 1 nbf = coio.nbfile(*os.pipe()) try: stackless.tasklet(Worker)('A', 5) stackless.tasklet(Worker)('B', 9) stackless.tasklet(Worker)('C', 7) for i in xrange(10): stackless.schedule() self.assertEqual('ABCABCABCABCABCBCBCBB', ''.join(events)) #self.assertEqual([nbf], coio.CurrentMainLoop().nbfs) finally: nbf.close()
#try: # rdata = coio.gethostbyname_ex('en.wikipedia.org') #except socket.gaierror, e: # rdata = e #logging.info(repr(rdata)) #logging.info('Query2 done.') listener_nbs = coio.new_realsocket(socket.AF_INET, socket.SOCK_STREAM) listener_nbs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener_nbs.bind(('127.0.0.1', 6666)) # Reducing this has a strong negative effect on ApacheBench worst-case # connection times, as measured with: # ab -n 100000 -c 50 http://127.0.0.1:6666/ >ab.stackless3.txt # It increases the maximum Connect time from 8 to 9200 milliseconds. listener_nbs.listen(100) if use_http: logging.info('visit http://%s:%s/' % listener_nbs.getsockname()) if use_https: upgrade_ssl_callback = wsgi.SslUpgrader( {'certfile': os.path.join(os.path.dirname(__file__), 'ssl_cert.pem'), 'keyfile': os.path.join(os.path.dirname(__file__), 'ssl_key.pem')}, use_http) logging.info('visit https://%s:%s/' % listener_nbs.getsockname()) else: upgrade_ssl_callback = None coio.stackless.tasklet(wsgi_listener)( listener_nbs, demo_wsgiapp.WsgiApp, upgrade_ssl_callback) std_nbf = coio.nbfile(0, 1, write_buffer_limit=2) ChatWorker(std_nbf, nbf_to_close=listener_nbs)
#except socket.gaierror, e: # rdata = e #logging.info(repr(rdata)) #logging.info('Query2 done.') listener_nbs = coio.new_realsocket(socket.AF_INET, socket.SOCK_STREAM) listener_nbs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener_nbs.bind(('127.0.0.1', 6666)) # Reducing this has a strong negative effect on ApacheBench worst-case # connection times, as measured with: # ab -n 100000 -c 50 http://127.0.0.1:6666/ >ab.stackless3.txt # It increases the maximum Connect time from 8 to 9200 milliseconds. listener_nbs.listen(100) if use_http: logging.info('visit http://%s:%s/' % listener_nbs.getsockname()) if use_https: upgrade_ssl_callback = wsgi.SslUpgrader( { 'certfile': os.path.join(os.path.dirname(__file__), 'ssl_cert.pem'), 'keyfile': os.path.join(os.path.dirname(__file__), 'ssl_key.pem') }, use_http) logging.info('visit https://%s:%s/' % listener_nbs.getsockname()) else: upgrade_ssl_callback = None coio.stackless.tasklet(wsgi_listener)(listener_nbs, demo_wsgiapp.WsgiApp, upgrade_ssl_callback) std_nbf = coio.nbfile(0, 1, write_buffer_limit=2) ChatWorker(std_nbf, nbf_to_close=listener_nbs)