Python tornado.gen.with_timeout() Examples

The following are 30 code examples of tornado.gen.with_timeout(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.gen , or try the search function .
Example #1
Source File: gen_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_gc(self):
        # Github issue 1769: Runner objects can get GCed unexpectedly
        # while their future is alive.
        weakref_scope = [None]  # type: List[Optional[weakref.ReferenceType]]

        def callback():
            gc.collect(2)
            weakref_scope[0]().set_result(123)  # type: ignore

        @gen.coroutine
        def tester():
            fut = Future()  # type: Future[int]
            weakref_scope[0] = weakref.ref(fut)
            self.io_loop.add_callback(callback)
            yield fut

        yield gen.with_timeout(datetime.timedelta(seconds=0.2), tester()) 
Example #2
Source File: test_tornado.py    From gremlinclient with MIT License 6 votes vote down vote up
def test_maxsize_release(self):
        pool = Pool("ws://localhost:8182/",
                    maxsize=2,
                    username="stephen",
                    password="password")
        c1 = yield pool.acquire()
        c2 = yield pool.acquire()
        c3 = pool.acquire()
        self.assertIsInstance(c3, Future)
        with self.assertRaises(tornado.gen.TimeoutError):
            yield gen.with_timeout(timedelta(seconds=0.1), c3)
        yield pool.release(c2)
        c3 = yield c3
        self.assertEqual(c2, c3)
        c1.conn.close()
        c2.conn.close()
        c3.conn.close() 
Example #3
Source File: TTornado.py    From Aditmadzs2 with GNU General Public License v3.0 6 votes vote down vote up
def open(self, timeout=None):
        logger.debug('socket connecting')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)

        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)

        raise gen.Return(self) 
Example #4
Source File: test_tornado_PEP492.py    From gremlinclient with MIT License 6 votes vote down vote up
def test_maxsize_release(self):
        pool = Pool("ws://localhost:8182/",
                    maxsize=2,
                    username="stephen",
                    password="password",
                    future_class=Future)

        async def go():
            c1 = await pool.acquire()
            c2 = await pool.acquire()
            c3 = pool.acquire()
            self.assertIsInstance(c3, Future)
            with self.assertRaises(gen.TimeoutError):
                await gen.with_timeout(timedelta(seconds=0.1), c3)
            await pool.release(c2)
            c3 = await c3
            self.assertEqual(c2, c3)
            c1.conn.close()
            c2.conn.close()
            c3.conn.close()

        self.loop.run_sync(go) 
Example #5
Source File: TTornado.py    From ajs2 with GNU General Public License v3.0 6 votes vote down vote up
def open(self, timeout=None):
        logger.debug('socket connecting')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)

        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)

        raise gen.Return(self) 
Example #6
Source File: test_tornado_PEP492.py    From gremlinclient with MIT License 6 votes vote down vote up
def test_maxsize(self):
        pool = Pool("ws://localhost:8182/",
                    maxsize=2,
                    username="stephen",
                    password="password",
                    loop=self.loop,
                    future_class=Future)

        async def go():
            c1 = await pool.acquire()
            c2 = await pool.acquire()
            c3 = pool.acquire()
            self.assertIsInstance(c3, Future)
            with self.assertRaises(gen.TimeoutError):
                await gen.with_timeout(timedelta(seconds=0.1), c3)
            c1.conn.close()
            c2.conn.close()

        self.loop.run_sync(go) 
Example #7
Source File: locks.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
        """Block until the internal flag is true.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()  # type: Future[None]
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(
                timeout, fut, quiet_exceptions=(CancelledError,)
            )
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(
                lambda tf: fut.cancel() if not fut.done() else None
            )
            return timeout_fut 
Example #8
Source File: TTornado.py    From galaxy-sdk-python with Apache License 2.0 6 votes vote down vote up
def open(self, timeout=None):
        logger.debug('socket connecting')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)

        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)

        raise gen.Return(self) 
Example #9
Source File: TTornado.py    From SOLO with GNU General Public License v3.0 6 votes vote down vote up
def open(self, timeout=None):
        logger.debug('koneksi ke server')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)
        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)
        raise gen.Return(self) 
Example #10
Source File: repo_handler.py    From codo-task with GNU General Public License v3.0 6 votes vote down vote up
def post(self, *args, **kwargs):
        git_list = []
        with DBContext('r') as session:
            git_conf = session.query(GitConf).all()

        for msg in git_conf:
            data_dict = model_to_dict(msg)
            git_list.append(data_dict)

        try:
            # 超过60s 返回Timeout
            res = yield gen.with_timeout(datetime.timedelta(seconds=60), self.sync_git_info(git_list),
                                         quiet_exceptions=gen.TimeoutError)

            return self.write(dict(code=0, msg=res))
        except gen.TimeoutError:
            return self.write(dict(code=-1, msg='TimeOut')) 
Example #11
Source File: server.py    From tornado_http2 with Apache License 2.0 6 votes vote down vote up
def _read_first_line(self, stream, address):
        try:
            header_future = stream.read_until_regex(b'\r?\n\r?\n',
                                                    max_bytes=self.conn_params.max_header_size)
            if self.conn_params.header_timeout is None:
                header_data = yield header_future
            else:
                try:
                    header_data = yield gen.with_timeout(
                        stream.io_loop.time() + self.conn_params.header_timeout,
                        header_future,
                        quiet_exceptions=StreamClosedError)
                except gen.TimeoutError:
                    stream.close()
                    return
            # TODO: make this less hacky
            stream._read_buffer[:0] = header_data
            stream._read_buffer_size += len(header_data)
            if header_data == b'PRI * HTTP/2.0\r\n\r\n':
                self._start_http2(stream, address)
            else:
                super(CleartextHTTP2Server, self)._start_http1(stream, address)
        except (StreamClosedError, UnsatisfiableReadError):
            pass 
Example #12
Source File: evaluation_plane_handler.py    From TabPy with MIT License 6 votes vote down vote up
def _call_subprocess(self, function_to_evaluate, arguments):
        restricted_tabpy = RestrictedTabPy(
            self.protocol, self.port, self.logger, self.eval_timeout
        )
        # Exec does not run the function, so it does not block.
        exec(function_to_evaluate, globals())

        # 'noqa' comments below tell flake8 to ignore undefined _user_script
        # name - the name is actually defined with user script being wrapped
        # in _user_script function (constructed as a striong) and then executed
        # with exec() call above.
        if arguments is None:
            future = self.executor.submit(_user_script,  # noqa: F821
                                          restricted_tabpy)
        else:
            future = self.executor.submit(_user_script,  # noqa: F821
                                          restricted_tabpy, **arguments)

        ret = yield gen.with_timeout(timedelta(seconds=self.eval_timeout), future)
        raise gen.Return(ret) 
Example #13
Source File: locks.py    From teleport with Apache License 2.0 6 votes vote down vote up
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
        """Block until the internal flag is true.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()  # type: Future[None]
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(
                timeout, fut, quiet_exceptions=(CancelledError,)
            )
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(
                lambda tf: fut.cancel() if not fut.done() else None
            )
            return timeout_fut 
Example #14
Source File: locks.py    From teleport with Apache License 2.0 6 votes vote down vote up
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
        """Block until the internal flag is true.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()  # type: Future[None]
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(
                timeout, fut, quiet_exceptions=(CancelledError,)
            )
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(
                lambda tf: fut.cancel() if not fut.done() else None
            )
            return timeout_fut 
Example #15
Source File: net_tornado.py    From rethinkdb-python with Apache License 2.0 6 votes vote down vote up
def with_absolute_timeout(deadline, generator, **kwargs):
    if deadline is None:
        res = yield generator
    else:
        try:
            res = yield gen.with_timeout(deadline, generator, **kwargs)
        except gen.TimeoutError:
            raise ReqlTimeoutError()
    raise gen.Return(res)


# The Tornado implementation of the Cursor object:
# The `new_response` Future notifies any waiting coroutines that the can attempt
# to grab the next result.  In addition, the waiting coroutine will schedule a
# timeout at the given deadline (if provided), at which point the future will be
# errored. 
Example #16
Source File: TTornado.py    From Protect4 with GNU General Public License v3.0 6 votes vote down vote up
def open(self, timeout=None):
        logger.debug('socket connecting')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)

        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)

        raise gen.Return(self) 
Example #17
Source File: main.py    From mftp with GNU General Public License v3.0 6 votes vote down vote up
def run_updates():
    def func():
        # try:
        #     print 'Checking companies...'
        #     update.check_companies()
        # except Exception as e:
        #     print e
        try:
            print 'Checking notices...'
            update.check_notices()
        except:
            print "Unhandled error occured :\n{}".format(traceback.format_exc())

    try:
        with ThreadPoolExecutor(max_workers=1) as executor:
            yield gen.with_timeout(datetime.timedelta(UPDATE_PERIOD/1000.0),
                                   executor.submit(func))
        print 'run_updates done'
    except gen.TimeoutError:
        print 'run_updates timed out' 
Example #18
Source File: locks.py    From teleport with Apache License 2.0 6 votes vote down vote up
def wait(self, timeout=None):
        """Block until the internal flag is true.

        Returns a Future, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(timeout, fut, quiet_exceptions=(CancelledError,))
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(lambda tf: fut.cancel() if not fut.done() else None)
            return timeout_fut 
Example #19
Source File: locks.py    From pySINDy with MIT License 6 votes vote down vote up
def wait(self, timeout=None):
        """Block until the internal flag is true.

        Returns a Future, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(timeout, fut, quiet_exceptions=(CancelledError,))
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(lambda tf: fut.cancel() if not fut.done() else None)
            return timeout_fut 
Example #20
Source File: locks.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
        """Block until the internal flag is true.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        """
        fut = Future()  # type: Future[None]
        if self._value:
            fut.set_result(None)
            return fut
        self._waiters.add(fut)
        fut.add_done_callback(lambda fut: self._waiters.remove(fut))
        if timeout is None:
            return fut
        else:
            timeout_fut = gen.with_timeout(
                timeout, fut, quiet_exceptions=(CancelledError,)
            )
            # This is a slightly clumsy workaround for the fact that
            # gen.with_timeout doesn't cancel its futures. Cancelling
            # fut will remove it from the waiters list.
            timeout_fut.add_done_callback(
                lambda tf: fut.cancel() if not fut.done() else None
            )
            return timeout_fut 
Example #21
Source File: gen_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_completes_before_timeout(self):
        future = Future()
        self.io_loop.add_timeout(datetime.timedelta(seconds=0.1),
                                 lambda: future.set_result('asdf'))
        result = yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                        future, io_loop=self.io_loop)
        self.assertEqual(result, 'asdf') 
Example #22
Source File: test_tornado.py    From gremlinclient with MIT License 5 votes vote down vote up
def test_maxsize(self):
        pool = Pool("ws://localhost:8182/",
                    maxsize=2,
                    username="stephen",
                    password="password")
        c1 = yield pool.acquire()
        c2 = yield pool.acquire()
        c3 = pool.acquire()
        self.assertIsInstance(c3, Future)
        with self.assertRaises(tornado.gen.TimeoutError):
            yield gen.with_timeout(timedelta(seconds=0.1), c3)
        c1.conn.close()
        c2.conn.close() 
Example #23
Source File: TTornado.py    From Aditmadzs2 with GNU General Public License v3.0 5 votes vote down vote up
def with_timeout(self, timeout, future):
        return gen.with_timeout(timeout, future, self.io_loop) 
Example #24
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_no_ref(self):
        # In this usage, there is no direct hard reference to the
        # WaitIterator itself, only the Future it returns. Since
        # WaitIterator uses weak references internally to improve GC
        # performance, this used to cause problems.
        yield gen.with_timeout(datetime.timedelta(seconds=0.1),
                               gen.WaitIterator(gen.sleep(0)).next()) 
Example #25
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_normal_concurrent_future(self):
        # A conccurrent future that resolves while waiting for the timeout.
        with futures.ThreadPoolExecutor(1) as executor:
            yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                   executor.submit(lambda: time.sleep(0.01))) 
Example #26
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_completed_concurrent_future(self):
        # A concurrent future that is resolved before we even submit it
        # to with_timeout.
        with futures.ThreadPoolExecutor(1) as executor:
            f = executor.submit(lambda: None)
            f.result()  # wait for completion
            yield gen.with_timeout(datetime.timedelta(seconds=3600), f) 
Example #27
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_timeout_concurrent_future(self):
        # A concurrent future that does not resolve before the timeout.
        with futures.ThreadPoolExecutor(1) as executor:
            with self.assertRaises(gen.TimeoutError):
                yield gen.with_timeout(self.io_loop.time(),
                                       executor.submit(time.sleep, 0.1)) 
Example #28
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_already_resolved(self):
        future = Future()
        future.set_result('asdf')
        result = yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                        future)
        self.assertEqual(result, 'asdf') 
Example #29
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_completes_before_timeout(self):
        future = Future()
        self.io_loop.add_timeout(datetime.timedelta(seconds=0.1),
                                 lambda: future.set_result('asdf'))
        result = yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                        future)
        self.assertEqual(result, 'asdf') 
Example #30
Source File: gen_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_timeout(self):
        with self.assertRaises(gen.TimeoutError):
            yield gen.with_timeout(datetime.timedelta(seconds=0.1),
                                   Future())