def main(verbose=0): global exitval, msg default_exitval = exitval default_msg = msg reports = [] failures = 0 # 3.0 is a multiple of 1.0, which timing allows a latent interrupt. for sleep_val in [2.5, 3.0]: coro.spawn (tripwire, sleep_val) coro.event_loop () reports.append ((sleep_val, msg, exitval)) if exitval != 0: failures += 1 # reset values: coro._exit = 0 exitval = default_exitval msg = default_msg if failures: if verbose: print "%d failures" % (failures,) for sleep_val, msg, exitval in reports: print "sleep val %s: %s" % (sleep_val, msg) sys.exit (1) else: if verbose: print "All tests passed." sys.exit (0)
def go (self): # main hoovering thread. # first, get a list of blocks we need to fetch via getheaders. db = G.block_db if not db.num_block: self.queue.push (block_db.genesis_block_hash) self.qset.add (block_db.genesis_block_hash) try: self.running = True # get the list of all blocks we need to fetch... while 1: c = self.get_live_connection() G.log ('getheaders', 'start') t0 = block_db.timer() names = c.getheaders() G.log ('getheaders', 'stop', '%.02f' % t0.end()) if names: break else: coro.sleep_relative (5) # start fetching them... coro.spawn (self.drain_queue_thread) for name in names: self.in_flight_sem.acquire(1) self.queue.push (name) self.qset.add (name) self.queue.push (None) finally: self.running = False
def main (args, global_state): global G G = global_state G.args = args if args.testnet: network.goto_testnet() if args.logfile: logger = coro.log.asn1.Logger (open (args.logfile, 'ab')) else: logger = coro.log.StderrLogger() coro.log.set_logger (logger) coro.log.redirect_stderr() if args.daemon: if not args.logfile: raise ValueError ("--daemon without --logfile") else: daemonize() open('/var/run/caesure.pid', 'wb').write ('%d\n' % os.getpid()) coro.set_exception_notifier (exception_notifier) LOG ('caesure', 'starting') coro.spawn (main1, args, G) try: coro.event_loop() except: LOG.exc() finally: LOG ('caesure', 'stopping')
def do_test(test_func): global q, finished q = circular_fifo() finished = 0 coro.spawn(test_func) while not finished: coro.sleep_relative(1)
def serve(port): s = coro.tcp_sock() s.bind(("", port)) s.listen(5) while 1: conn, addr = s.accept() coro.spawn(go, conn, addr)
def test_local(self): """Test thread-local storage.""" self.t1_cv = coro.condition_variable() self.t2_cv = coro.condition_variable() self.shared = coro.ThreadLocal() self.shared.x = 1 t1 = coro.spawn(self.t1) t2 = coro.spawn(self.t2) # Let them run. coro.yield_slice() self.t1_cv.wake_one() # Let t1 run. coro.yield_slice() self.assertEqual(self.shared.x, 1) self.t2_cv.wake_one() # Let t2 run. coro.yield_slice() self.assertEqual(self.shared.x, 1) self.t1_cv.wake_one() self.t2_cv.wake_one() coro.yield_slice() t1.join() t2.join() self.assertEqual(self.shared.x, 1) del self.shared
def smtp_tls_server(): sock = coro.make_socket (socket.AF_INET, socket.SOCK_STREAM) sock.bind (('0.0.0.0', 25)) sock.listen (5) while True: conn, addr = sock.accept() coro.spawn (smtp_tls_server_session, conn)
def serve (port=9000): s = coro.tcp_sock() s.bind (('', port)) s.listen (50) while 1: conn, addr = s.accept() coro.spawn (session, conn, addr)
def multi_test(): t1 = coro.spawn(tak2, 18, 12, 6) t2 = coro.spawn(tak2, 18, 12, 6) t3 = coro.spawn(tak2, 18, 12, 6) t1.join() t2.join() t3.join()
def start_aio(self): self._start_time = coro.get_now() self.main_thread_state = 'Starting writers.' self.log(1, 'Starting %i writers.', self.options.num_writers) for x in xrange(self.options.num_writers): self._worker_semaphore.acquire() coro.spawn(self._writer, x) while 1: # Spin lock. status = 'Waiting for writers to ramp up %i/%i.' % (self._writes_finished, self.options.reader_delay) self.main_thread_state = status self.log(2, status) if int(self._worker_semaphore) == 0: self.log(0, 'EEP! All writers exited too fast.') self.main_thread_state = 'Aborted.' return if self._writes_finished < self.options.reader_delay: coro.sleep_relative(0.5) else: break self.main_thread_state = 'Starting readers.' self.log(1, 'Starting %i readers.', self.options.num_readers) for x in xrange(self.options.num_readers): self._worker_semaphore.acquire() coro.spawn(self._reader, x) self.main_thread_state = 'Waiting for all threads to finish.' self.log(1, 'Waiting till all threads finished.') self._worker_semaphore.block_till_zero() self.main_thread_state = 'Done.'
def smtp_tls_server(): sock = coro.make_socket (socket.AF_INET, socket.SOCK_STREAM) sock.bind (('0.0.0.0', 25)) sock.listen (5) while 1: conn, addr = sock.accept() coro.spawn (smtp_tls_server_session, conn)
def start_aio(self): self._start_time = coro.get_now() self.main_thread_state = 'Starting writers.' self.log(1, 'Starting %i writers.', self.options.num_writers) for x in xrange(self.options.num_writers): self._worker_semaphore.acquire() coro.spawn(self._writer, x) while True: # Spin lock. status = 'Waiting for writers to ramp up %i/%i.' % (self._writes_finished, self.options.reader_delay) self.main_thread_state = status self.log(2, status) if int(self._worker_semaphore) == 0: self.log(0, 'EEP! All writers exited too fast.') self.main_thread_state = 'Aborted.' return if self._writes_finished < self.options.reader_delay: coro.sleep_relative(0.5) else: break self.main_thread_state = 'Starting readers.' self.log(1, 'Starting %i readers.', self.options.num_readers) for x in xrange(self.options.num_readers): self._worker_semaphore.acquire() coro.spawn(self._reader, x) self.main_thread_state = 'Waiting for all threads to finish.' self.log(1, 'Waiting till all threads finished.') self._worker_semaphore.block_till_zero() self.main_thread_state = 'Done.'
def test_mutex(): m.lock() coro.spawn(locker, 1) coro.spawn(locker, 2) coro.yield_and_schedule() m.unlock() coro.yield_and_schedule() coro._exit = 1
def serve(port): s = coro.make_socket(socket.AF_INET, socket.SOCK_STREAM) s.set_reuse_addr() s.bind(('', port)) s.listen(8192) while 1: conn, addr = s.accept() coro.spawn(service_client, conn, addr)
def __init__ (self, root, addr, channel_factory=Server_Channel): self.root = root self.addr = addr self.channel_factory = channel_factory self.socket = self.make_socket() self.socket.setsockopt (socket.SOL_SOCKET, socket.SO_SNDBUF, self.buffer_size) self.socket.setsockopt (socket.SOL_SOCKET, socket.SO_RCVBUF, self.buffer_size) coro.spawn (self.serve)
def serve (saddr): ip, port = saddr s = coro.tcp_sock() s.bind (('0.0.0.0', port + 9000)) s.listen (5) while 1: conn, caddr = s.accept() coro.spawn (session, conn, caddr, saddr)
def notify_height(self, conn, height): if height > self.target: # XXX sanity check height by calculating a likely neighborhood given the date. behind = height - self.target self.target = height if not self.running: # start hoovering coro.spawn(self.go, conn)
def notify_height (self, conn, height): if height > self.target: # XXX sanity check height by calculating a likely neighborhood given the date. behind = height - self.target self.target = height if not self.running: # start hoovering coro.spawn (self.go, conn)
def serve(saddr): ip, port = saddr s = coro.tcp_sock() s.bind(('0.0.0.0', port + 9000)) s.listen(5) while 1: conn, caddr = s.accept() coro.spawn(session, conn, caddr, saddr)
def test_with_timeout_with_interrupt(): coro.spawn(_test_with_timeout_with_interrupt, coro.current()) try: coro.print_stderr(' Should be no pause:\n') coro.with_timeout(1, coro.sleep_relative, 5) except coro.Interrupted, why: if why != 'foobar': raise ValueError('Interrupt value is not foobar!')
def serve (port): s = coro.make_socket (socket.AF_INET, socket.SOCK_STREAM) s.set_reuse_addr() s.bind (('', port)) s.listen (8192) while 1: conn, addr = s.accept() coro.spawn (service_client, conn, addr)
def server(port=18080): s = coro.coroutine_socket() s.create_socket(socket.AF_INET, socket.SOCK_STREAM) s.set_reuse_addr() s.bind(('', port)) s.listen(1024) while 1: conn, addr = s.accept() coro.spawn(client, conn, addr)
def test_coro_main(self): coro.set_print_exit_string(None) for x in range(4): try: coro.spawn(exit_coro) coro.event_loop() except SystemExit: pass
def test_sleep_interrupt(): coro.spawn(_test_sleep_interrupt, coro.current()) # The other thread will interrupt me coro.print_stderr('There should be no pause here:\n') try: coro.sleep_relative(10) except coro.Interrupted, why: if why != 'foobar': raise ValueError('Incorrect interrupt value.')
def test_raise(): # Resume myself. This should work without problems. coro.spawn(_test_raise, coro.current()) try: result = coro.yield() except ZeroDivisionError, why: if why[0] != 12345: raise ValueError('Why is wrong %s' % (why,)) pass
def server(port=18080): s = coro.coroutine_socket() s.create_socket(socket.AF_INET, socket.SOCK_STREAM) s.set_reuse_addr() s.bind(("", port)) s.listen(1024) while True: conn, addr = s.accept() coro.spawn(client, conn, addr)
def server(port=9090): "echo server" server = coro_ssl.ssl_sock() server.create() server.bind(('', port)) server.listen(1024) while True: conn, addr = server.accept() coro.spawn(channel, conn, addr)
def server (port=9090): "echo server" server = coro_ssl.ssl_sock() server.create() server.bind (('', port)) server.listen (1024) while True: conn, addr = server.accept() coro.spawn (channel, conn, addr)
def __init__(self, conn, addr, saddr): self.conn = conn self.addr = addr self.saddr = saddr self.id = session.counter session.counter += 1 self.proxy = coro.tcp_sock() self.proxy.connect(saddr) coro.spawn(self.feed, self.conn, self.proxy, '<==') coro.spawn(self.feed, self.proxy, self.conn, '==>')
def drain_queue_thread (self): while 1: name = self.queue.pop() if name is None: break else: LOG ('hoover', 'popped', repr(name)) self.qset.remove (name) c = self.get_live_connection() coro.spawn (self.get_block, c, name)
def __init__ (self, my_addr, other_addr, sock=None): BaseConnection.__init__ (self, my_addr, other_addr, sock) self.last_packet = 0 if sock is not None: self.direction = 'incoming' else: self.direction = 'outgoing' self.waiting = {} self.known = set() coro.spawn (self.go)
def drain_queue_thread(self): while 1: name = self.queue.pop() if name is None: break else: LOG('hoover', 'popped', repr(name)) self.qset.remove(name) c = self.get_live_connection() coro.spawn(self.get_block, c, name)
def test_unlock_resume(self): """Test that unlock resume.""" m = coro.mutex() coro.spawn(self._test_unlock_resume, m) coro.yield_slice() # This will block, bounce over to other thread. self.assertTrue(m.lock()) self.assertTrue(m.has_lock()) self.assertFalse(m.unlock()) self.assertFalse(m.has_lock())
def __init__(self, root, addr, channel_factory=Server_Channel): self.root = root self.addr = addr self.channel_factory = channel_factory self.socket = self.make_socket() self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.buffer_size) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.buffer_size) coro.spawn(self.serve)
def serve (port=9000): s = coro.ssl.sock (ctx) s.bind (('0.0.0.0', port)) s.listen (50) print 's=', s while 1: conn, addr = s.accept() print 'conn, addr=', conn, addr all_conns.append (conn) coro.spawn (session, conn, addr)
def __init__ (self, channel, queue='', uuid_fun=uuid.uuid1): self.channel = channel if queue == '': self.queue = channel.queue_declare (exclusive=True).queue else: self.queue = queue self.uuid_fun = uuid_fun self.consumer = self.channel.basic_consume (queue=self.queue) self.pending = {} coro.spawn (self.reply_thread)
def __init__(self, channel, queue='', uuid_fun=uuid.uuid1): self.channel = channel if queue == '': self.queue = channel.queue_declare(exclusive=True).queue else: self.queue = queue self.uuid_fun = uuid_fun self.consumer = self.channel.basic_consume(queue=self.queue) self.pending = {} coro.spawn(self.reply_thread)
def __init__ (self, conn, addr, saddr): self.conn = conn self.addr = addr self.saddr = saddr self.id = session.counter session.counter += 1 self.proxy = coro.tcp_sock() self.proxy.connect (saddr) coro.spawn (self.feed, self.conn, self.proxy, '<==') coro.spawn (self.feed, self.proxy, self.conn, '==>')
def serve(self, address): self.s = coro.make_socket_for_ip(address, socket.SOCK_STREAM) self.s.bind((address, 0)) self.bound_ip, self.port = self.s.getsockname() self.s.set_reuse_addr() self.s.listen(5) s, addr = self.s.accept() session = TestSession(s, addr) coro.spawn(session.run) self.accepted_from = s.getsockname()[0]
def frame_syn_stream(self, flags, data): sid, asid, pri = struct.unpack(">LLH", data[:10]) # XXX do something with priority sid &= 0x7FFFFFFF asid &= 0x7FFFFFFF # W ('syn_stream: sid=%d asid=%d pri=%x ' % (sid, asid, pri)) headers = self.unpack_http_header(data[10:]) req = spdy_server_request(flags, sid, self, headers) # W ('%s\n' % req.request,) self.streams[sid] = req coro.spawn(self.handle_request, req)
def run(self): self.streams = {} self.deflate = deflator() self.inflate = inflator() self.ofifo = coro.fifo() self.obuf = coro.semaphore(self.output_buffer_size) coro.spawn(self.send_thread) try: self.read_frames() finally: self.ofifo.push(None)
def __init__ (self, host, port=80, conn=None, inflight=100): self.host = host self.inflight = coro.semaphore (inflight) if conn is None: self.conn = coro.tcp_sock() self.conn.connect ((host, port)) else: self.conn = conn self.stream = coro.read_stream.sock_stream (self.conn) self.pending = coro.fifo() coro.spawn (self.read_thread)
def generate_random_field(self): for i in range(5000): c = random.choice(colors) x = random.randint(0, self.w - 100) y = random.randint(0, self.h - 100) w = random.randint(50, 300) h = random.randint(50, 300) b = box(c, (x, y, x + w, y + h)) self.Q_back.insert(b) for i in range(1000): coro.spawn(self.wanderer)
def request(self, path, params): if self.state == self.CLOSED: # closed raise RPC_Connection_Closed elif self.state != self.CONNECTED: if self.state == self.UNCONNECTED: coro.spawn(self.get_connected) self.connection_cv.wait() return Channel.request(self, path, params)
def frame_syn_stream(self, flags, data): sid, asid, pri = struct.unpack('>LLH', data[:10]) # XXX do something with priority sid &= 0x7fffffff asid &= 0x7fffffff # W ('syn_stream: sid=%d asid=%d pri=%x ' % (sid, asid, pri)) headers = self.unpack_http_header(data[10:]) req = spdy_server_request(flags, sid, self, headers) # W ('%s\n' % req.request,) self.streams[sid] = req coro.spawn(self.handle_request, req)
def handle_request (self, request): sid, fifo = self.find_session (request) if fifo is None: # login fifo = coro.fifo() fifo.push (request) sid = self.gen_session_id() request['set-cookie'] = 'session=%s' % (sid,) self.sessions[sid] = fifo coro.spawn (self.wrap, sid, fifo) else: fifo.push (request)
def start_lio(self): self._start_time = coro.get_now() self.main_thread_state = 'Starting LIO workers.' self.log(1, 'Starting %i lio workers.', self.options.num_lio_workers) for x in xrange(self.options.num_lio_workers): self._worker_semaphore.acquire() coro.spawn(self._lio, x) self.main_thread_state = 'Waiting for all threads to finish.' self.log(1, 'Waiting till all threads finished.') self._worker_semaphore.block_till_zero() self.main_thread_state = 'Done.'