Esempio n. 1
0
 def test(self):
     try:
         sock = socket.socket()
         core.timer(0, sock.close)
         sock.connect(('python.org', 81))
     except Exception:
         gevent.sleep(0)
     else:
         assert False, 'expected an error here'
Esempio n. 2
0
 def test(self):
     try:
         sock = socket.socket()
         core.timer(0, sock.close)
         sock.connect(('python.org', 81))
     except Exception:
         gevent.sleep(0)
     else:
         assert False, 'expected an error here'
Esempio n. 3
0
 def start(self):
     """Schedule the timeout."""
     assert not self.pending, '%r is already started; to restart it, cancel it first' % self
     if self.seconds is None: # "fake" timeout (never expires)
         self.timer = None
     elif self.exception is None or self.exception is False: # timeout that raises self
         self.timer = core.timer(self.seconds, getcurrent().throw, self)
     else: # regular timeout with user-provided exception
         self.timer = core.timer(self.seconds, getcurrent().throw, self.exception)
Esempio n. 4
0
 def update(self, event, evtype):
     if evtype & core.EV_READ:
         self.read.append(event.arg)
         if self.timer is None:
             self.timer = core.timer(0, self.event.set)
     elif evtype & core.EV_WRITE:
         self.write.append(event.arg)
         if self.timer is None:
             self.timer = core.timer(0, self.event.set)
Esempio n. 5
0
 def update(self, event, evtype):
     if evtype & core.EV_READ:
         self.read.append(event.arg)
         if self.timer is None:
             self.timer = core.timer(0, self.event.set)
     elif evtype & core.EV_WRITE:
         self.write.append(event.arg)
         if self.timer is None:
             self.timer = core.timer(0, self.event.set)
Esempio n. 6
0
 def start(self):
     """Schedule the timeout."""
     assert not self.pending, '%r is already started; to restart it, cancel it first' % self
     if self.seconds is None:  # "fake" timeout (never expires)
         self.timer = None
     elif self.exception is None or self.exception is False:  # timeout that raises self
         self.timer = core.timer(self.seconds, getcurrent().throw, self)
     else:  # regular timeout with user-provided exception
         self.timer = core.timer(self.seconds, getcurrent().throw, self.exception)
Esempio n. 7
0
def waitany(objects, timeout=None):
    waiter = Waiter()
    switch = waiter.switch

    if not objects:
        return None

    timer = None
    if timeout is not None:
        timer = core.timer(timeout, switch, _NONE)

    try:
        for obj in objects:
            obj.rawlink(switch)

        rst = waiter.get()
        return None if rst is _NONE else rst

    finally:
        timer and timer.cancel()

        for obj in objects:
            unlink = getattr(obj, 'unlink', None)
            if unlink:
                try:
                    unlink(switch)
                except:
                    import traceback
                    traceback.print_exc()
Esempio n. 8
0
    def _reset_timer(self):
        assert self._timer is None, "Trying to reset active timer!"

        t = time()
        s = self.get_setting('interval', opt_type=int)
        if self.normalize:
            s = (s - (t % s))

        self._timer = timer(s, self._pool.spawn, self.run)
Esempio n. 9
0
    def test_sleep(self):
        # even if there was an error in the mainloop, the hub should continue to work
        start = time.time()
        gevent.sleep(DELAY)
        delay = time.time() - start

        assert delay >= DELAY * 0.9, 'sleep returned after %s seconds (was scheduled for %s)' % (delay, DELAY)

        def fail():
            raise greentest.ExpectedException('TestExceptionInMainloop.test_sleep/fail')

        core.timer(0, fail)

        start = time.time()
        gevent.sleep(DELAY)
        delay = time.time() - start

        assert delay >= DELAY * 0.9, 'sleep returned after %s seconds (was scheduled for %s)' % (delay, DELAY)
Esempio n. 10
0
    def test_sleep(self):
        # even if there was an error in the mainloop, the hub should continue to work
        start = time.time()
        gevent.sleep(DELAY)
        delay = time.time() - start

        assert delay >= DELAY * 0.9, 'sleep returned after %s seconds (was scheduled for %s)' % (
            delay, DELAY)

        def fail():
            raise greentest.ExpectedException(
                'TestExceptionInMainloop.test_sleep/fail')

        core.timer(0, fail)

        start = time.time()
        gevent.sleep(DELAY)
        delay = time.time() - start

        assert delay >= DELAY * 0.9, 'sleep returned after %s seconds (was scheduled for %s)' % (
            delay, DELAY)
Esempio n. 11
0
def sleep(seconds=0):
    """Put the current greenlet to sleep for at least *seconds*.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired. Calling sleep with *seconds* of 0 is the canonical way of
    expressing a cooperative yield.
    """
    unique_mark = object()
    timer = core.timer(seconds, getcurrent().switch, unique_mark)
    try:
        switch_result = get_hub().switch()
        assert switch_result is unique_mark, 'Invalid switch into sleep(): %r' % (
            switch_result, )
    except:
        timer.cancel()
        raise
Esempio n. 12
0
File: hub.py Progetto: SMFOSS/gevent
def sleep(seconds=0):
    """Put the current greenlet to sleep for at least *seconds*.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired. Calling sleep with *seconds* of 0 is the canonical way of
    expressing a cooperative yield.
    """
    unique_mark = object()
    if not seconds >= 0:
        raise IOError(22, 'Invalid argument')
    timer = core.timer(seconds, getcurrent().switch, unique_mark)
    try:
        switch_result = get_hub().switch()
        assert switch_result is unique_mark, 'Invalid switch into sleep(): %r' % (switch_result, )
    except:
        timer.cancel()
        raise
Esempio n. 13
0
 def func():
     core.timer(0.01, self.lst.pop)
     gevent.sleep(0.02)
Esempio n. 14
0
                client_socket = HiveSocket(_sock=client_socket)
                spawn = self._spawn
                if spawn is None:
                    self._handle(client_socket, address)
                else:
                    spawn(self._handle, client_socket, address).link_exception(receiver=self.exception_logger)
            except:
                traceback.print_exc()
                ex = sys.exc_info()[1]
                if self.is_fatal_error(ex):
                    self.kill()
                    sys.stderr.write('ERROR: %s failed with %s\n' % (self, str(ex) or repr(ex)))
                    return
                try:
                    if address is None:
                        sys.stderr.write('%s: Failed.\n' % (self, ))
                    else:
                        sys.stderr.write('%s: Failed to handle request from %s\n' % (self, address, ))
                except Exception:
                    traceback.print_exc()
                if self.delay >= 0:
                    self.stop_accepting()
                    self._start_accepting_timer = core.timer(self.delay, self.start_accepting)
                    self.delay = min(self.max_delay, self.delay * 2)
                return


def wrap_socket(*args, **kwargs):
    """Create a new :class:`SSLSocket` instance."""
    return HiveSSLSocket(*args, **kwargs)
Esempio n. 15
0
def schedule(time, f, *args, **kwargs):
    try:
        f(*args, **kwargs)
    finally:
        timer(time, schedule, time, f, *args, **kwargs)
                                     (self, str(ex) or repr(ex)))
                    return
                try:
                    if address is None:
                        sys.stderr.write('%s: Failed.\n' % (self, ))
                    else:
                        sys.stderr.write(
                            '%s: Failed to handle request from %s\n' % (
                                self,
                                address,
                            ))
                except Exception:
                    traceback.print_exc()
                if self.delay >= 0:
                    self.stop_accepting()
                    self._start_accepting_timer = core.timer(
                        self.delay, self.start_accepting)
                    self.delay = min(self.max_delay, self.delay * 2)
                return

    def is_fatal_error(self, ex):
        return isinstance(
            ex, socket.error) and ex[0] in (errno.EBADF, errno.EINVAL,
                                            errno.ENOTSOCK)

    def wrap_socket_and_handle(self, client_socket, address):
        # used in case of ssl sockets
        ssl_socket = self.wrap_socket(client_socket, **self.ssl_args)
        return self.handle(ssl_socket, address)


def _import_sslold_wrap_socket():
Esempio n. 17
0
 def start(self, *arg, **argv):
     timer(self.callback_time, self._run, *arg, **argv)
Esempio n. 18
0
 def start_later(self, seconds):
     """Schedule the greenlet to run in the future loop iteration *seconds* later"""
     assert self._start_event is None, 'Greenlet already started'
     self._start_event = core.timer(seconds, self.switch)
Esempio n. 19
0
 def wait(self, timeout):
     evt = core.timer(timeout, self.waiter.switch, None)
     try:
         return self.waiter.get()
     finally:
         evt.cancel()
Esempio n. 20
0
 def wait(self, timeout):
     evt = core.timer(timeout, self.waiter.switch, None)
     try:
         return self.waiter.get()
     finally:
         evt.cancel()