Esempio n. 1
0
 def test_with_timeout(self):
     self.assertRaises(gevent.Timeout, gevent.with_timeout, DELAY, gevent.sleep, DELAY * 2)
     X = object()
     r = gevent.with_timeout(DELAY, gevent.sleep, DELAY * 2, timeout_value=X)
     assert r is X, (r, X)
     r = gevent.with_timeout(DELAY * 2, gevent.sleep, DELAY, timeout_value=X)
     assert r is None, r
Esempio n. 2
0
def callpeers(peers):
    user, domain, password = None, None, None
    try:
        for peer in peers:
            peer = escape(peer)
            peer = 'sip:' + peer + '@gd.ctcims.cn'
            logger.info('calling ' + peer)
            (user, domain, password) = freeAccounts.get(timeout=maxwait)
            try:
                options = Options(user,
                                  domain,
                                  password,
                                  bac=bac,
                                  int_ip=int_ip,
                                  to=peer,
                                  uri=peer)
                caller = sipstackcaller.Caller(options)
                gevent.sleep(30)
                logger.info('hangup call to ' + str(caller.options.to))
                try:
                    gevent.with_timeout(10, caller.close)
                except Timeout:
                    logger.warn('caller.close timeout')
            except Exception as e:
                logger.warn(e)
            finally:
                if user and domain and password:
                    logger.info('freeAccounts.put')
                    freeAccounts.put((user, domain, password))
    except Queue.Empty:
        logger.warn('get from queue  timeout')
    except Exception as e:
        logger.warn(e)
Esempio n. 3
0
    def wait(self, timeout=None, die=False):
        """
        wait blocks until the task has been executed
        if timeout is specified and the task didn't finished within timeout seconds,
        raises TimeoutError

        if die is True and the state is TASK_STATE_ERROR after the wait, the eco of the exception will be raised
        """
        def wait():
            while self.state in ('new', 'running'):
                gevent.sleep(0.5)

        if timeout:
            # ensure the type is correct
            timeout = float(timeout)
            try:
                gevent.with_timeout(timeout, wait)
            except gevent.Timeout:
                raise TimeoutError()
        else:
            wait()

        if die is True and self.state == TASK_STATE_ERROR:
            if not self.eco:
                logger.critical('task is in error state, but no eco')
            else:
                raise self.eco

        return self
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')
Esempio n. 5
0
 def test_with_timeout(self):
     self.assertRaises(gevent.Timeout, gevent.with_timeout, SHOULD_EXPIRE, gevent.sleep, SHOULD_NOT_EXPIRE)
     X = object()
     r = gevent.with_timeout(SHOULD_EXPIRE, gevent.sleep, SHOULD_NOT_EXPIRE, timeout_value=X)
     assert r is X, (r, X)
     r = gevent.with_timeout(SHOULD_NOT_EXPIRE, gevent.sleep, SHOULD_EXPIRE, timeout_value=X)
     assert r is None, r
    def forward(self, message):
        attempts = 0
        isSucces = False

        for interval in self.RETRY_BACKOFF_INTERVALS:
            attempts += 1

            if not self._connected:
                self._connect()

            try:
                self._socket.send(message)
                gevent.with_timeout(self.RECEIVE_TIMEOUT, self._socket.recv)
            except (Timeout, ZMQError):
                self._disconnect()
                self._logForwardFailure(attempts, interval)

                gevent.sleep(interval)
                continue

            isSucces = True
            break

        if isSucces:
            self._log('Successfully forwarded message to %s in %d attempts' %
                      (self._address, attempts))
        else:
            self._log('Failed to forward message to %s in %d attemts' %
                      (self._address, attempts))
Esempio n. 7
0
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')
Esempio n. 9
0
 def test_with_timeout(self):
     self.assertRaises(gevent.Timeout, gevent.with_timeout, DELAY, gevent.sleep, DELAY * 2)
     X = object()
     r = gevent.with_timeout(DELAY, gevent.sleep, DELAY * 2, timeout_value=X)
     assert r is X, (r, X)
     r = gevent.with_timeout(DELAY * 2, gevent.sleep, DELAY, timeout_value=X)
     assert r is None, r
Esempio n. 10
0
    def forward(self, message):
        attempts = 0
        isSucces = False

        for interval in self.RETRY_BACKOFF_INTERVALS:
            attempts += 1

            if not self._connected:
                self._connect()

            try:
                self._socket.send(message)
                gevent.with_timeout(self.RECEIVE_TIMEOUT, self._socket.recv)
            except (Timeout, ZMQError):
                self._disconnect()
                self._logForwardFailure(attempts, interval)

                gevent.sleep(interval)
                continue

            isSucces = True
            break

        if isSucces:
            self._log('Successfully forwarded message to %s in %d attempts'
                      % (self._address, attempts))
        else:
            self._log('Failed to forward message to %s in %d attemts'
                      % (self._address, attempts))
Esempio n. 11
0
def test_signal():
    """
    tests graceful exit when in middle of 2 requests

    this test requires gevent monkey patched nosetest
    gets two pages in separate threads
    sends SIGUSR1
    verifies that now 503 page is received
    verifies that subprocess closes within threshold
    """
    gevent.sleep(1)
    g1 = gevent.spawn(get_page)
    gevent.sleep(1)
    g2 = gevent.spawn(get_page)
    gevent.sleep(1)
    processes[0].sigusr1()
    gevent.sleep(0.1)
    g3 = gevent.spawn(get_page_503)

    # raise_error passes exceptions (failed asserts) from greenlets
    gevent.joinall([g1, g2, g3], raise_error=True)
    try:
        gevent.with_timeout(2, processes[0].join)
        timeout_exception = False
    except gevent.Timeout:
        timeout_exception = True

    assert timeout_exception == False, 'server was not killed'
Esempio n. 12
0
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')
Esempio n. 13
0
    def testMySQLTimeout(self):
        cnn = client.connect(host=DB_HOST,
                             user=DB_USER,
                             password=DB_PASSWD,
                             db=DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()

        from gevent import Timeout

        start = time.time()
        try:

            def delay():
                cnn.query("select sleep(4)")
                self.fail('expected timeout')

            gevent.with_timeout(2, delay)
        except Timeout:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places=1)

        cnn.close()
Esempio n. 14
0
 def test_with_timeout(self):
     with self.assertRaises(gevent.Timeout):
         gevent.with_timeout(SHOULD_EXPIRE, gevent.sleep, SHOULD_NOT_EXPIRE)
     X = object()
     r = gevent.with_timeout(SHOULD_EXPIRE, gevent.sleep, SHOULD_NOT_EXPIRE, timeout_value=X)
     self.assertIs(r, X)
     r = gevent.with_timeout(SHOULD_NOT_EXPIRE, gevent.sleep, SHOULD_EXPIRE, timeout_value=X)
     self.assertIsNone(r)
Esempio n. 15
0
 def wait_eq(self, terminator, message=None, timeout=None):
     if self.value == terminator:
         return self.value
     self.terminator = terminator
     try:
         with_timeout(timeout, self.done.get)
         return self.value
     except Timeout:
         eq_(self.value, terminator, message)
Esempio n. 16
0
 def wait_eq(self, terminator, message=None, timeout=None):
     if self.value == terminator:
         return self.value
     self.terminator = terminator
     try:
         with_timeout(timeout, self.done.get)
         return self.value
     except Timeout:
         eq_(self.value, terminator, message)
Esempio n. 17
0
 def check_timed_out(self, event, myproc, proc_finished_flag, queue):
     assert with_timeout(DELAY, event.get, timeout_value=X) is X, repr(
         event.get())
     assert with_timeout(DELAY, queue.get,
                         timeout_value=X) is X, queue.get()
     assert with_timeout(DELAY, gevent.joinall, [myproc],
                         timeout_value=X) is X
     assert proc_finished_flag == [], proc_finished_flag
     myproc.kill()
Esempio n. 18
0
    def test_run(self):
        class T(TaskService):
            _outcome = 0

            def run(self):
                self._outcome = 1

        t = T("test_run")
        gevent.with_timeout(1, t.start_and_wait)
        self.assertEqual(t._outcome, 1)
Esempio n. 19
0
    def _run(self):
        while self.parent_service().keep_running:
            task = self.parent_service().task_queue.get()

            try:
                self.parent_service().notify_worker_active()
                gevent.with_timeout(task.timeout(self.task_timeout), task.execute)
            finally:
                self.parent_service().notify_worker_finished(
                    task.action_log_ids())
Esempio n. 20
0
    def _run(self):
        while self.parent_service().keep_running:
            task = self.parent_service().task_queue.get()

            try:
                self.parent_service().notify_worker_active()
                gevent.with_timeout(task.timeout(self.task_timeout), task.execute)
            finally:
                self.parent_service().notify_worker_finished(
                    task.action_log_ids())
Esempio n. 21
0
    def run(self):
        child = self.spawn(ExampleActor)

        while True:
            dbg("sending greeting to %r" % (child,))
            child << 'hello!'

            dbg("waiting for ack from %r" % (child,))
            with_timeout(5.0, self.get, 'ack')

            dbg("got 'ack' from %r; now sleeping a bit..." % (child,))
            sleep(1.0)
Esempio n. 22
0
 def connect(self):
     """Initiate a connection; will retry if enabled"""
     if not self.connected:
         try:
             gevent.with_timeout(self.timeout,
                     self._sock.connect, (self.host, self.port))
         except socket.error as e:
             return e.strerror
         except Timeout:
             self.disconnect(ERR_TIMEOUT)
         else:
             self.state = True
Esempio n. 23
0
    def run(self):
        child = self.spawn(ExampleActor)

        while True:
            dbg("sending greeting to %r" % (child, ))
            child << 'hello!'

            dbg("waiting for ack from %r" % (child, ))
            with_timeout(5.0, self.get, 'ack')

            dbg("got 'ack' from %r; now sleeping a bit..." % (child, ))
            sleep(1.0)
    def _run(self):
        while True:

            assert not self.connected

            self._req_socket = self._context.socket(zmq.REQ)
            self._req_socket.setsockopt(zmq.LINGER, 1000)
            self._log.debug("connecting to server")
            self._req_socket.connect(self._server_address)

            # send a handshake
            message_control = {
                "message-type": "resilient-server-handshake",
                "message-id": uuid.uuid1().hex,
                "client-tag": self._client_tag,
                "client-address": self._client_address,
            }
            self._req_socket.send_json(message_control)

            # wait for  an ack
            ack_reply = gevent.with_timeout(_ack_timeout, self._req_socket.recv_json, timeout_value=None)
            if ack_reply is None:
                error_message = "timeout waiting handshake ack: retry {0} seconds".format(_handshake_retry_interval)
                self._log.error(error_message)
                self._req_socket.close()
                self._req_socket = None
                gevent.sleep(_handshake_retry_interval)
                continue

            self.connected = True

            while self.connected:

                # block until we get a message to send
                message_to_send = self._send_queue.get()

                self._send_message(message_to_send)

                # wait for  an ack
                ack_reply = gevent.with_timeout(_ack_timeout, self._req_socket.recv_json, timeout_value=None)
                if ack_reply is None:
                    error_message = "timeout waiting ack: treating as disconnect"
                    self._log.error(error_message)
                    self._req_socket.close()
                    self._req_socket = None

                    self.connected = False

                    self._deliver_failure_reply(message_to_send)

                    gevent.sleep(_handshake_retry_interval)
                    break
Esempio n. 25
0
def maingreenlet(inputdict, timeout, outputmode, validation_func):
    rg = Worker(inputdict, timeout, outputmode, validation_func)
    i = 0
    try:
        gevent.with_timeout(5, rg.start, i)
    except Timeout:
        print 'Exception of timeout'
	rg.stop()
	print 'Exiting all greenlets'
    except TaskComplete:
        print "Task complete message from queue" ,rg.queue.get()
    except:
	print "Hahahahah"
Esempio n. 26
0
def maingreenlet():
    test_class = Worker()
    i = 0
    try:
        gevent.with_timeout(5, test_class.start, i)
    except Timeout:
        print 'Exception of timeout'
        test_class.stop()
        print 'Exiting all greenlets'
    except TaskComplete:
        print "Task complete message from queue", test_class.queue.get()
    except:
        print "Error: Unknown exception occured", sys.exc_info()
Esempio n. 27
0
    def _run(self):
        while self.parent_service().keep_running:
            task = self.parent_service().task_queue.get()

            try:
                self.parent_service().notify_worker_active()
                gevent.with_timeout(task.timeout(self.task_timeout), task.execute)
            except:
                self.log.error('SyncbackWorker caught exception', exc_info=True,
                               account_id=task.account_id)
            finally:
                self.parent_service().notify_worker_finished(
                    task.action_log_ids)
Esempio n. 28
0
 def test_with_timeout(self):
     with self.assertRaises(gevent.Timeout):
         gevent.with_timeout(SHOULD_EXPIRE, gevent.sleep, SHOULD_NOT_EXPIRE)
     X = object()
     r = gevent.with_timeout(SHOULD_EXPIRE,
                             gevent.sleep,
                             SHOULD_NOT_EXPIRE,
                             timeout_value=X)
     self.assertIs(r, X)
     r = gevent.with_timeout(SHOULD_NOT_EXPIRE,
                             gevent.sleep,
                             SHOULD_EXPIRE,
                             timeout_value=X)
     self.assertIsNone(r)
Esempio n. 29
0
 def test_with_timeout(self):
     self.assertRaises(gevent.Timeout, gevent.with_timeout, SHOULD_EXPIRE,
                       gevent.sleep, SHOULD_NOT_EXPIRE)
     X = object()
     r = gevent.with_timeout(SHOULD_EXPIRE,
                             gevent.sleep,
                             SHOULD_NOT_EXPIRE,
                             timeout_value=X)
     assert r is X, (r, X)
     r = gevent.with_timeout(SHOULD_NOT_EXPIRE,
                             gevent.sleep,
                             SHOULD_EXPIRE,
                             timeout_value=X)
     assert r is None, r
Esempio n. 30
0
    def _run(self):
        while self.parent_service().keep_running:
            task = self.parent_service().task_queue.get()

            try:
                self.parent_service().notify_worker_active()
                gevent.with_timeout(task.timeout(self.task_timeout),
                                    task.execute)
            except:
                self.log.error('SyncbackWorker caught exception',
                               exc_info=True,
                               account_id=task.account_id)
            finally:
                self.parent_service().notify_worker_finished(
                    task.action_log_ids)
Esempio n. 31
0
    def do_req(self, req, addr):
        authres = self.proxy_auth(req)
        if authres is not None:
            res.sendto(req.stream)
            return res

        req.url = urlparse(req.uri)
        if req.method.upper() == 'CONNECT':
            hostname, func = req.url.path, proxy.connect
            tout = self.config.get('conn_tout')
        else:
            if not req.url.netloc:
                logger.info('manager %s' % (req.url.path, ))
                res = self.srv_urls.get(req.url.path, mgr_default)(self, req)
                res.sendto(req.stream)
                return res
            hostname, func = req.url.netloc, proxy.http
            tout = self.config.get('http_tout')

        usesocks = self.usesocks(hostname.split(':', 1)[0])
        reqinfo = (req, usesocks, addr, time.time())
        with self.with_worklist(reqinfo):
            logger.info(fmt_reqinfo(reqinfo))
            if not tout: return func(req, self.get_conn_mgr(not usesocks))
            try:
                return with_timeout(tout, func, req,
                                    self.get_conn_mgr(not usesocks))
            except Timeout, err:
                logger.warn('connection timeout: %s' % req.uri)
Esempio n. 32
0
def PerformOperationWithTimeout(timeout_seconds, fn, *args, **kwargs):
  """Calls a function with the given arguments, but times out after
  timeout_seconds have elapsed. The function is called in the same greenlet. On
  a timeout an exception is thrown and caught.

  NOTE: The function must cooperatively yield (via non-blocking I/O or an
  explicit gevent.sleep() call).

  Args:
    timeout_seconds - How long, in float seconds, to give the function before
        timing out and returning
    fn - A Python callable to invoke.
    *args, **kwargs, The arguments to pass to 'fn'.

  Returns:
    A (return_value, timed_out) tuple, where 'return_value' is the value
    returned from the called function ('fn'), and 'timed_out' is a boolean value
    indicating whether 'fn' timed out when called.  If 'timed_out' is True,
    'return_value' is None.
  """
  try:
    returned_value = gevent.with_timeout(timeout_seconds, fn, *args, **kwargs)
  except Timeout:
    return (None, True)

  # Small workaround for a feature of with_timeout which returns the value
  # supplied with the kwarg timeout_value. If this value was returned then the
  # function timedout without throwing Timeout.
  if 'timeout_value' in kwargs and returned_value == kwargs['timeout_value']:
    return (None, True)

  return (returned_value, False)
Esempio n. 33
0
 def get(self, request, format=None, id=None, slug=None):
     queryset = self.get_queryset()
     conn = self.get_connection()
     if 'id' in request.query_params:
         seen_id = request.query_params['id']
         try:
             old_timestamp = queryset.get(seen_id).run(conn)['timestamp']
         except:
             return Response({'id': 'invalid event id'},
                             status=status.HTTP_400_BAD_REQUEST)
         queryset = queryset.filter(lambda row: (row[
             'timestamp'] >= old_timestamp) & (row['id'] != seen_id))
     if id is not None:
         queryset = queryset.filter({'asset_id': id})
     elif slug is not None:
         queryset = queryset.filter({'service_tag': slug})
     results = list(queryset.order_by("timestamp").run(conn))
     if len(results) == 0:
         feed = queryset.changes().filter({'old_val': None}).run(conn)
         try:
             results = [
                 gevent.with_timeout(settings.SOCRATES_CHANGEFEED_MAX_WAIT,
                                     feed.next)['new_val']
             ]
         except gevent.Timeout:
             return Response(status=status.HTTP_204_NO_CONTENT)
     response = Response(results)
     return response
Esempio n. 34
0
    def do_req(self, req, addr):
        authres = self.proxy_auth(req)
        if authres is not None:
            authres.sendto(req.stream)
            return authres

        req.url = urlparse(req.uri)
        if req.method.upper() == 'CONNECT':
            hostname, func = req.url.path, proxy.connect
            tout = self.config.get('conn_tout')
        else:
            if not req.url.netloc:
                logger.info('manager %s' % (req.url.path,))
                res = self.srv_urls.get(req.url.path, mgr_default)(self, req)
                res.sendto(req.stream)
                return res
            hostname, func = req.url.netloc, proxy.http
            tout = self.config.get('http_tout')

        usesocks = self.usesocks(hostname.split(':', 1)[0])
        reqinfo = (req, usesocks, addr, time.time())
        with self.with_worklist(reqinfo):
            logger.info(fmt_reqinfo(reqinfo))
            if not tout: return func(req, self.get_conn_mgr(not usesocks))
            try:
                return with_timeout(
                    tout, func, req, self.get_conn_mgr(not usesocks))
            except Timeout, err:
                logger.warn('connection timeout: %s' % req.uri)
Esempio n. 35
0
 def _do_gai(family, proto=0, flags=0):
     _log.debug("_do_gai: started family=%s, proto=%d, flags=%s", family,
                proto, flags)
     try:
         addrs = gevent.with_timeout(
             dns_timeout,
             socket.getaddrinfo,
             host,
             port,
             family,
             socket.SOCK_STREAM,
             proto,
             flags,
         )
         _log.debug("_do_gai: finished family=%s, addrs=%r", family, addrs)
         while addrs:
             (*_, addr) = addrs.pop()
             bus.put((0, (family, addr)))
     except _Cancel:
         _log.debug("_do_gai: cancelled family=%s", family)
     except gevent.Timeout:
         bus.put((-1, (family, None,
                       socket.gaierror(-errno.ETIMEDOUT, "Timed out"))))
     except Exception as e:
         bus.put((-1, (family, None, e)))
Esempio n. 36
0
    def run(self, once=False, timeout=None):
        if timeout and not once:
            raise RuntimeError(
                "using a receive timeout value without once=True")

        try:
            return self._events.get_nowait()
        except queue.Empty:
            pass

        if self._running:
            raise RuntimeError("already running")
        self._running = True
        try:
            while not self._shutdown.is_set():
                # TODO: we might need to iwait for a shutdown event?
                # though realistically nothing outside methods invoked by
                # _feed -> XMLParser sets _shutdown so?
                buf = gevent.with_timeout(timeout, self.sock.recv,
                                          MAX_RECV_BUF)
                if not buf:
                    break
                self._feed(buf)
                if not once:
                    continue
                try:
                    return self._events.get_nowait()
                except queue.Empty:
                    continue
            exc_info = self.reset()
            if exc_info:
                reraise(*exc_info)
            return None
        finally:
            self._running = False
Esempio n. 37
0
def PerformOperationWithTimeout(timeout_seconds, fn, *args, **kwargs):
  """Calls a function with the given arguments, but times out after
  timeout_seconds have elapsed. The function is called in the same greenlet. On
  a timeout an exception is thrown and caught.

  NOTE: The function must cooperatively yield (via non-blocking I/O or an
  explicit gevent.sleep() call).

  Args:
    timeout_seconds - How long, in float seconds, to give the function before
                      timing out and returning
    fn - A Python callable to invoke.
    *args, **kwargs, The arguments to pass to 'fn'.

  Returns:
    A (return_value, timed_out) tuple, where 'return_value' is the value
    returned from the called function ('fn'), and 'timed_out' is a boolean value
    indicating whether 'fn' timed out when called.  If 'timed_out' is True,
    'return_value' is unspecified.
  """
  try:
    returned_value = gevent.with_timeout(timeout_seconds, fn, *args, **kwargs)
  except Timeout:
    return (None, True)

  # Small workaround for a feature of with_timeout which returns the value
  # supplied with the kwarg timeout_value. If this value was returned then the
  # function timedout without throwing Timeout.
  if 'timeout_value' in kwargs and returned_value == kwargs['timeout_value']:
    return (None, True)

  return (returned_value, False)
Esempio n. 38
0
File: g.py Progetto: dmsnell/circus
class Dealer(Base):
    __zmq_socket_type__ = zmq.DEALER
    __zmq_method__ = 'connect'
    __zmq_direction__ = BOTH
    __zmq_multipart__ = True

    def init(self, timeout=2):
        self._calls = {}
        self.timeout = timeout

    def handle(self, message):
        _id, response = message
        if _id in self._calls:
            self._calls.pop(_id).set(response)
        if self.stopped:
            if not self._calls:
                self.stopped.set()

    def send(self, message):
        if self.stopped:
            raise Exception("stopped.")
        _id = uuid.uuid4().bytes
        event = self._calls[_id] = gevent.event.AsyncResult()
        self._send([_id, message])
        try:
            self._recv(zmq.NOBLOCK)
        except ZMQError, e:
            if e.errno == zmq.EAGAIN: pass
            else: raise
        try:
            ret = gevent.with_timeout(self.timeout, event.get)
        except gevent.timeout.Timeout, e:
            self._calls.pop(_id)
            raise TimeoutException()
Esempio n. 39
0
    def configure_main(self, config_name, action, contents):
        """
        The main configuration callback.

        :param config_name:
        :param action:
        :param contents:
        """

        self.vip.pubsub.subscribe('pubsub', topics.PLATFORM_SEND_EMAIL, self.on_email_message)

        self.vip.pubsub.subscribe('pubsub', topics.ALERTS_BASE,self.on_alert_message)
        self.vip.pubsub.subscribe('pubsub',prefix=topics.ALERTS.format(agent_class='',agent_uuid=''),callback=self.on_alert_message)
        self.current_config = self.default_config.copy()
        self.current_config.update(contents)

        self.current_config['allow_frequency_seconds'] = self.current_config.get(
            'allow_frequency_minutes', 60) * 60

        smtp_address = self.current_config.get('smtp_address', None)
        smtp_port = self.current_config.get('smtp_port', None)
        smtp_username = self.current_config.get('smtp_username', None)
        smtp_password = self.current_config.get('smtp_password', None)
        if action == "UPDATE":
            try:
                with gevent.with_timeout(3, self._test_smtp_address, smtp_address,smtp_port,smtp_username,smtp_password):
                    pass
            except Exception as e:
                self.vip.health.set_status(STATUS_BAD, "Invalid SMTP Address")
Esempio n. 40
0
 def test_kill_fires_once(self):
     u1 = Undead()
     u2 = Undead()
     p1 = gevent.spawn(u1)
     p2 = gevent.spawn(u2)
     def check(count1, count2):
         assert p1, p1
         assert p2, p2
         assert not p1.dead, p1
         assert not p2.dead, p2
         self.assertEqual(u1.shot_count, count1)
         self.assertEqual(u2.shot_count, count2)
     gevent.sleep(0.01)
     s = pool.GreenletSet([p1, p2])
     assert len(s) == 2, s
     check(0, 0)
     s.killone(p1, block=False)
     check(0, 0)
     gevent.sleep(0)
     check(1, 0)
     s.killone(p1)
     check(1, 0)
     s.killone(p1)
     check(1, 0)
     s.kill(block=False)
     s.kill(block=False)
     s.kill(block=False)
     check(1, 0)
     gevent.sleep(DELAY)
     check(1, 1)
     X = object()
     kill_result = gevent.with_timeout(DELAY, s.kill, block=True, timeout_value=X)
     assert kill_result is X, repr(kill_result)
     assert len(s) == 2, s
     check(1, 1)
Esempio n. 41
0
 def _players(rpc_client):
     try:
         return with_timeout(3, rpc_client.user_players,
             sns, sns_id,
             timeout_value=None)
     except:
         return None
Esempio n. 42
0
    def test_set_with_timeout(self):
        event2 = AsyncResult()

        X = object()
        result = gevent.with_timeout(DELAY, event2.get, timeout_value=X)
        self.assertIs(
            result, X,
            'Nobody sent anything to event2 yet it received %r' % (result, ))
Esempio n. 43
0
    def test_set_with_timeout(self):
        event2 = AsyncResult()

        X = object()
        result = gevent.with_timeout(DELAY, event2.get, timeout_value=X)
        self.assertIs(
            result, X,
            'Nobody sent anything to event2 yet it received %r' % (result, ))
Esempio n. 44
0
def main():
    person = Person()
    response = gevent.with_timeout(3, person.return_number_if_count_5, timeout_value=3)
    if response>0:
        print "Some result came out"
    else:
        print "No results :("

    print "End of Main"
Esempio n. 45
0
 def _players(rpc_client):
     try:
         return with_timeout(3,
                             rpc_client.user_players,
                             sns,
                             sns_id,
                             timeout_value=None)
     except:
         return None
Esempio n. 46
0
 def test_set(self):
     g = gevent.spawn(lambda: 1)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     assert s1.get() == 1
     assert s2.get() == 1
     assert gevent.with_timeout(DELAY, s3.get, timeout_value=X) is X
Esempio n. 47
0
 def test_set(self):
     g = gevent.spawn(lambda: 1)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     assert s1.get() == 1
     assert s2.get() == 1
     assert gevent.with_timeout(DELAY, s3.get, timeout_value=X) is X
Esempio n. 48
0
def run_fuzzer(bind, parsed_arguments, collection_name):
    """ This is the main gevent thread for the odfuzz process.)

    :param bind: # Argument 'bind' can be used for binding the standard ooutput of this process instance to another process, e.g. celery (ODfuzz-server)
    :param parsed_arguments:
    :param collection_name:
    :return:
    """
    manager = Manager(bind, parsed_arguments, collection_name)
    try:
        if parsed_arguments.timeout == INFINITY_TIMEOUT:
            manager.start()
        else:
            gevent.with_timeout(parsed_arguments.timeout, manager.start)
    except ODfuzzException as ex:
        sys.stderr.write(str(ex) + '\n')
        sys.exit(1)
    except gevent.Timeout:
        signal_handler(collection_name)
Esempio n. 49
0
    def test_gz_logfile(self):
        ftp = self.login()
        logfile = 'tests/data/1308216000.gz'
        f = open(logfile)
        ftp.storbinary('STOR test_logfile_%s.gz' % time.time(), f)

        event = gevent.with_timeout(5,
                                    self.batch_processor_in.recv,
                                    timeout_value=None)
        eq_(event['parser'], 'SyslogParser')
        eq_(event['sid'], 'ftpc|127.0.0.1-ubuntu')
Esempio n. 50
0
 def test_set_exception(self):
     def func():
         raise greentest.ExpectedException('TestAsyncResultAsLinkTarget.test_set_exception')
     g = gevent.spawn(func)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     self.assertRaises(greentest.ExpectedException, s1.get)
     assert gevent.with_timeout(DELAY, s2.get, timeout_value=X) is X
     self.assertRaises(greentest.ExpectedException, s3.get)
Esempio n. 51
0
 def stop_agent(self, agent_uuid):
     try:
         execenv = self.agents[agent_uuid]
     except KeyError:
         return
     if execenv.process.poll() is None:
         execenv.process.send_signal(signal.SIGINT)
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             execenv.process.terminate()
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             execenv.process.kill()
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             raise ValueError('process is unresponsive')
     return execenv.process.poll()
Esempio n. 52
0
 def test_set_exception(self):
     def func():
         raise greentest.ExpectedException('TestAsyncResultAsLinkTarget.test_set_exception')
     g = gevent.spawn(func)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     self.assertRaises(greentest.ExpectedException, s1.get)
     assert gevent.with_timeout(DELAY, s2.get, timeout_value=X) is X
     self.assertRaises(greentest.ExpectedException, s3.get)
Esempio n. 53
0
 def test_set(self):
     g = gevent.spawn(lambda: 1)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     self.assertEqual(s1.get(), 1)
     self.assertEqual(s2.get(), 1)
     X = object()
     result = gevent.with_timeout(DELAY, s3.get, timeout_value=X)
     self.assertIs(result, X)
Esempio n. 54
0
    def testMySQLTimeout(self):
        cnn = amysql.Connection()
        cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs.rows)

        from gevent import Timeout

        start = time.time()
        try:
            def delay():
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
            gevent.with_timeout(2, delay)
        except Timeout:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)

        cnn.close()
Esempio n. 55
0
 def test_set(self):
     g = gevent.spawn(lambda: 1)
     s1, s2, s3 = AsyncResult(), AsyncResult(), AsyncResult()
     g.link(s1)
     g.link_value(s2)
     g.link_exception(s3)
     self.assertEqual(s1.get(), 1)
     self.assertEqual(s2.get(), 1)
     X = object()
     result = gevent.with_timeout(DELAY, s3.get, timeout_value=X)
     self.assertIs(result, X)
Esempio n. 56
0
 def stop_agent(self, agent_name):
     if '/' in agent_name:
         agent_name = os.path.abspath(agent_name)
     execenv = self.agents.get(agent_name)
     if not execenv:
         return
     if execenv.process.poll() is None:
         execenv.process.send_signal(signal.SIGINT)
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             execenv.process.terminate()
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             execenv.process.kill()
         try:
             return gevent.with_timeout(3, process_wait, execenv.process)
         except gevent.Timeout:
             raise ValueError('process is unresponsive')
     return execenv.process.poll()
Esempio n. 57
0
    def testMySQLTimeout(self):
        cnn = client.connect(host = DB_HOST, user = DB_USER,
                             password = DB_PASSWD, db = DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()

        from gevent import Timeout

        start = time.time()
        try:
            def delay():
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
            gevent.with_timeout(2, delay)
        except Timeout:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)

        cnn.close()
Esempio n. 58
0
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()
Esempio n. 59
0
 def run(self):
     while self.keepGoing:
         response = Response()
         response.start = time.time()
         try:
             def performCall():
                 http = urllib2.urlopen(self.master.url)
                 return http.read()
                 
             bytes = gevent.with_timeout(self.master.maxLatency, performCall)
             response.contentLength = len(bytes)
         except BaseException, e:
             response.error = str(e)
         finally: