def real_done(self): if not self.script_path or not self.ip or not self.port: return { 'status': 'down', 'msg': 'checker argc error' + self.log_detail() } _ret = {'status': 'up', 'msg': 'good'} timeout = Timeout.start_new(self.checker_timeout) try: _ret = self._checker() except Timeout as e: logger.warning("checker timeout, %s", self.log_detail(e)) _ret['msg'] = 'time out' except Exception as e: logger.error("checker script run error, %s", self.log_detail(e)) _ret['msg'] = 'script run error' finally: timeout.cancel() logger.info("checker ip[%s] port[%d] status[%s] msg[%s]", self.ip, self.port, _ret.get('status'), _ret.get('msg')) return _ret
def foo3(): # 对各种Greenlet和数据结构相关的调用,gevent也提供了超时参数 def wait(): gevent.sleep(2) timer = Timeout(5).start() # 5s没跑完就抛出Timeout异常 thread1 = gevent.spawn(wait) try: thread1.join(timeout=timer) except Timeout: print('Thread 1 timed out') else: print('Thread 1 complete') timer = Timeout.start_new(1) thread2 = gevent.spawn(wait) try: thread2.get(timeout=timer) except Timeout: print('Thread 2 timed out') try: gevent.with_timeout(1, wait) except Timeout: print('Thread 3 timed out')
def main(): timer = Timeout(1).start() thread1 = gevent.spawn(wait) try: thread1.join(timeout = timer) except Timeout: print('Thread1 timed out') # -- timer = Timeout.start_new(1) thread2 = gevent.spawn(wait) try: thread2.get(timeout = timer) except Timeout: print('thread2 timedout') # -- try: gevent.with_timeout(1, wait) except Timeout: print('thread 3 timeout')
def main(): timer = Timeout(1).start() thread1 = gevent.spawn(wait) try: thread1.join(timeout=timer) except Timeout: print('Thread1 timed out') # -- timer = Timeout.start_new(1) thread2 = gevent.spawn(wait) try: thread2.get(timeout=timer) except Timeout: print('thread2 timedout') # -- try: gevent.with_timeout(1, wait) except Timeout: print('thread 3 timeout')
def wait(self, timeout=None): if self.ready(): return self.value else: switch = getcurrent().switch self.rawlink(switch) try: timer = Timeout.start_new(timeout) if timeout is not None else None try: getattr(getcurrent(), 'awaiting_batch', lambda: None)() result = get_hub().switch() assert result is self, 'Invalid switch into AsyncResult.wait(): %r' % (result, ) finally: if timer is not None: timer.cancel() except Timeout as exc: self.unlink(switch) if exc is not timer: raise except: self.unlink(switch) raise # not calling unlink() in non-exception case, because if switch() # finished normally, link was already removed in _notify_links return self.value
def test2(): timer = Timeout.start_new(3) # constrain executing greenlet thread = gevent.spawn(task) try: thread.get(timeout=timer) except Timeout: print('could not complete')
def start(self): """Start the periodic task. Warning: Do not call this function directly. It should instead by called by a TaskManager instance or some other object that can keep track of running Tasks. Raises: TimeoutError: when a Task's runtime exceeds its per-run limit for maximum execution time. Exception: for all other cases. Returns: None """ if self.running: self.logger.warning("task is already running") elif self._g: self.logger.error("task has already claimed Greenlet") else: self._schedule = True # do not schedule this task to run if not self._schedule: self.logger.debug('task is not being scheduled to run') return if not self._event.is_set(): self.logger.debug('skipping task run, event is not set') self.__callback(None) return # handle a potential timeout if self._timeout_obj: self._timeout_obj.cancel() self._timeout_obj = None if self._timeout_secs > 0: self._timeout_obj = Timeout.start_new(self._timeout_secs, TimeoutError) try: self._g = self.__make() self.timing.start() self.pool.start(self._g) self._running = True except TimeoutError as e: self.logger.warning(e) self.__callback(self._g) except Exception as e: self.logger.exception(e, exc_info=True) raise TaskRuntimeError from e
def tpump(input, output, chunk_size): size = 0 while True: try: timeout = Timeout.start_new(5) chunk = input.read(chunk_size) timeout.cancel() except Timeout: input.release_conn() raise if not chunk: break output.write(chunk) size += len(chunk) return size
def _wait_ack(self): """ ack or timeout return: True -> current block is acked. False -> timeout waiting acknowledgement. """ timer = Timeout.start_new(self._timeout) try: while self.__wait_one_ack() != self._expected_block_num: # timer's ticking. pass return True except Timeout as e: return None finally: timer.cancel()
def _wait_data(self): """ wait a data or timeout """ timer = Timeout.start_new(self._timeout) try: while True: data = self._wait_one_block() if isinstance( data, Data ) and data.block_number == self._expected_block_num: break return data except Timeout as e: return None finally: timer.cancel()
def handle_api_request(socket, address): stats['api_request_cnt'] += 1 timeout = Timeout.start_new(rpc_timeout) try: req = msgpack_unpack_msg_from_socket(socket) logger.info('%s %s', address, req['endpoint']) handler = api_request_handlers.get(req['endpoint']) if handler is not None: handler(socket, address, req) else: logger.info('-> %s unknown request', address) socket.sendall(msgpack.packb('UNKNOWN')) except Timeout: stats['api_request_timeout_cnt'] += 1 logger.info('-> %s timeout', address) socket.sendall(msgpack.packb('TIMEOUT')) finally: timeout.cancel() socket.close()
def sample_addition(): timer = Timeout(1).start() thread1 = gevent.spawn(wait_2sec) try: thread1.join(timeout=timer) except Timeout: print 'Thread 1 time out' timer = Timeout.start_new(1) thread2 = gevent.spawn(wait_2sec) try: thread2.get(timeout=timer) except Timeout: print 'Thread 2 time out' try: gevent.with_timeout(1, wait_2sec) except Timeout: print 'Thread 3 time out' gevent.spawn(wait_2sec).get()
def handle_api_request(socket, address): metrics.incr('api_request_cnt') timeout = Timeout.start_new(rpc_timeout) try: req = msgpack_unpack_msg_from_socket(socket) if not req: logger.warning('Couldn\'t get msgpack data from %s', address) socket.close() return access_logger.info('%s %s', address, req['endpoint']) handler = api_request_handlers.get(req['endpoint']) if handler is not None: handler(socket, address, req) else: logger.info('-> %s unknown request', address) socket.sendall(msgpack.packb('UNKNOWN')) except Timeout: metrics.incr('api_request_timeout_cnt') logger.warning('-> %s timeout', address) socket.sendall(msgpack.packb('TIMEOUT')) finally: timeout.cancel() socket.close()
def join(self, timeout=None): """Wait until the greenlet finishes or *timeout* expires. Return ``None`` regardless. """ if self.ready(): return switch = getcurrent().switch self.rawlink(switch) try: t = Timeout.start_new(timeout) try: getattr(getcurrent(), 'awaiting_batch', lambda: None)() result = self.parent.switch() assert result is self, 'Invalid switch into Greenlet.join(): %r' % (result, ) finally: t.cancel() except Timeout as ex: self.unlink(switch) if ex is not t: raise except: self.unlink(switch) raise
from gevent import Timeout def wait( ): gevent.sleep( 2 ) timer = Timeout( 1 ).start() thread1 = gevent.spawn( wait ) try: thread1.join( timeout=timer ) except Timeout: print( "Thread 1 timed out" ) # -- timer = Timeout.start_new( 1 ) thread2 = gevent.spawn( wait ) try: thread2.get( timeout=timer ) except Timeout: print( "Thread 2 timed out" ) # -- try: gevent.with_timeout( 1, wait ) except Timeout: print( "Thread 3 timed out" )
def wait(): gevent.sleep(2) timer = Timeout(1).start() thread1 = gevent.spawn(wait) try: print('Thread 1 started') thread1.join(timeout=timer) except Timeout: print('Thread 1 timed out') # -- timer = Timeout.start_new(1) thread2 = gevent.spawn(wait) try: print('Thread 2 started') thread2.get(timeout=timer) except Timeout: print('Thread 2 timed out') # -- try: print('Thread 3 started') gevent.with_timeout(1, wait) except Timeout: print('Thread 3 timed out')
def _decorator(*args,**kwargs): try: timer = Timeout.start_new(timeout,exception) timer.start() ret = func(*args,**kwargs)
def wait(): gevent.sleep(2) timer = Timeout(1).start() thread1 = gevent.spawn(wait) # 这种超时类型前面讲过 try: thread1.join(timeout=timer) except Timeout: print("Thread 1 timed out") timer = Timeout.start_new(1) # start_new是一个快捷方式 thread2 = gevent.spawn(wait) try: thread2.get(timeout=timer) # get返回greenlet的结果,包含异常 except Timeout: print("Thread 2 timed out") try: gevent.with_timeout(1, wait) # 如果超时前返回异常,取消这个方法 except Timeout: print("Thread 3 timed out")