當前位置: 首頁>>代碼示例>>Python>>正文


Python asyncio.shield方法代碼示例

本文整理匯總了Python中asyncio.shield方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.shield方法的具體用法?Python asyncio.shield怎麽用?Python asyncio.shield使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在asyncio的用法示例。


在下文中一共展示了asyncio.shield方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: downstream

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def downstream(self):
        try:
            self.upstream_buffer_task = \
                    asyncio.ensure_future(self.consume_upstream_buffer())
            async for msg in self.up_conn:
                if msg.type == aiohttp.WSMsgType.TEXT:
                    await self.down_conn.send_str(msg.data)
                    if self.downstream_cb is not None:
                        await asyncio.shield(self.downstream_cb(msg.data))
                if msg.type == aiohttp.WSMsgType.BINARY:
                    await self.down_conn.send_bytes(msg.data)
                    if self.downstream_cb is not None:
                        await asyncio.shield(self.downstream_cb(msg.data))
                elif msg.type == aiohttp.WSMsgType.CLOSED:
                    break
                elif msg.type == aiohttp.WSMsgType.ERROR:
                    break
            # here, server gracefully disconnected
        except asyncio.CancelledError:
            raise
        except Exception:
            log.exception('unexpected error')
        finally:
            await self.close_upstream() 
開發者ID:lablup,項目名稱:backend.ai-manager,代碼行數:26,代碼來源:wsproxy.py

示例2: _redirect_async

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def _redirect_async(self, redirect, auth):
        """Redirect the client endpoint using a Link DETACH redirect
        response.

        :param redirect: The Link DETACH redirect details.
        :type redirect: ~uamqp.errors.LinkRedirect
        :param auth: Authentication credentials to the redirected endpoint.
        :type auth: ~uamqp.authentication.common.AMQPAuth
        """
        # pylint: disable=protected-access
        if not self._connection._cbs:
            _logger.info("Closing non-CBS session.")
            await asyncio.shield(self._session.destroy_async(), loop=self.loop)
        self._session = None
        self._auth = auth
        self._hostname = self._remote_address.hostname
        await self._connection.redirect_async(redirect, auth)
        await self._build_session_async() 
開發者ID:Azure,項目名稱:azure-uamqp-python,代碼行數:20,代碼來源:client_async.py

示例3: _client_run_async

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def _client_run_async(self):
        """MessageSender Link is now open - perform message send
        on all pending messages.
        Will return True if operation successful and client can remain open for
        further work.

        :rtype: bool
        """
        # pylint: disable=protected-access
        await self.message_handler.work_async()
        self._waiting_messages = 0
        async with self._pending_messages_lock:
            self._pending_messages = await self._filter_pending_async()
        if self._backoff and not self._waiting_messages:
            _logger.info("Client told to backoff - sleeping for %r seconds", self._backoff)
            await self._connection.sleep_async(self._backoff)
            self._backoff = 0
        await asyncio.shield(self._connection.work_async(), loop=self.loop)
        return True 
開發者ID:Azure,項目名稱:azure-uamqp-python,代碼行數:21,代碼來源:client_async.py

示例4: send_offsets_to_transaction

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def send_offsets_to_transaction(self, offsets, group_id):
        self._ensure_transactional()

        if not self._txn_manager.is_in_transaction():
            raise IllegalOperation("Not in the middle of a transaction")

        if not group_id or not isinstance(group_id, str):
            raise ValueError(group_id)

        # validate `offsets` structure
        formatted_offsets = commit_structure_validate(offsets)

        log.debug(
            "Begin adding offsets %s for consumer group %s to transaction",
            formatted_offsets, group_id)
        fut = self._txn_manager.add_offsets_to_txn(formatted_offsets, group_id)
        await asyncio.shield(fut, loop=self._loop) 
開發者ID:robinhood,項目名稱:aiokafka,代碼行數:19,代碼來源:producer.py

示例5: _listener

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def _listener(self, subscription, interval, callback):
        while True:
            try:
                # FIXME: This should use an async requester as below
                packet = callback()
                self.__handle_packet(packet, subscription)
                await asyncio.sleep(interval)
            except asyncio.CancelledError:
                ## unsubscribe from all subscriptions
                confirmations = await asyncio.gather(
                    asyncio.shield(self._unsubscribe(subscription)) 
                    for subscription in self.subscriptions)
            except Exception as ex:
                logger.error(ex)
                logger.error(packet)
                raise 
開發者ID:snth,項目名稱:numismatic,代碼行數:18,代碼來源:base.py

示例6: disconnect

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def disconnect(self, timeout: Optional[float] = None) -> None:
        """Close the connection gracefully."""
        await self._connection.close(code=1000)
        try:
            await asyncio.wait_for(
                asyncio.shield(self._message_processor), timeout or self.TIMEOUT
            )
            self._message_processor.result()
        except asyncio.TimeoutError:
            pass
        finally:
            if not self._message_processor.done():
                self._message_processor.cancel()
                try:
                    await self._message_processor
                except asyncio.CancelledError:
                    pass 
開發者ID:datadvance,項目名稱:DjangoChannelsGraphqlWs,代碼行數:19,代碼來源:transport.py

示例7: init_workers

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def init_workers(app: web.Application, conf: WorkersConfig) -> ThreadPoolExecutor:
    n = conf.max_workers
    executor = ThreadPoolExecutor(max_workers=n)

    loop = asyncio.get_event_loop()
    run = loop.run_in_executor
    fs = [run(executor, warm, conf.path_to_model_state) for _ in range(0, n)]
    await asyncio.gather(*fs)

    async def close_executor(app: web.Application) -> None:
        fs = [run(executor, clean) for _ in range(0, n)]
        await asyncio.shield(asyncio.gather(*fs))
        executor.shutdown(wait=True)

    app.on_cleanup.append(close_executor)
    app["executor"] = executor
    return executor 
開發者ID:truskovskiyk,項目名稱:nima.pytorch,代碼行數:19,代碼來源:worker.py

示例8: test_shield_effect

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def test_shield_effect(self):
        # Cancelling outer() does not affect inner().
        proof = 0
        waiter = asyncio.Future(loop=self.loop)

        @asyncio.coroutine
        def inner():
            nonlocal proof
            yield from waiter
            proof += 1

        @asyncio.coroutine
        def outer():
            nonlocal proof
            yield from asyncio.shield(inner(), loop=self.loop)
            proof += 100

        f = asyncio.ensure_future(outer(), loop=self.loop)
        test_utils.run_briefly(self.loop)
        f.cancel()
        with self.assertRaises(asyncio.CancelledError):
            self.loop.run_until_complete(f)
        waiter.set_result(None)
        test_utils.run_briefly(self.loop)
        self.assertEqual(proof, 1) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:27,代碼來源:test_tasks.py

示例9: test_gather_shield

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def test_gather_shield(self):
        child1 = asyncio.Future(loop=self.loop)
        child2 = asyncio.Future(loop=self.loop)
        inner1 = asyncio.shield(child1, loop=self.loop)
        inner2 = asyncio.shield(child2, loop=self.loop)
        parent = asyncio.gather(inner1, inner2, loop=self.loop)
        test_utils.run_briefly(self.loop)
        parent.cancel()
        # This should cancel inner1 and inner2 but bot child1 and child2.
        test_utils.run_briefly(self.loop)
        self.assertIsInstance(parent.exception(), asyncio.CancelledError)
        self.assertTrue(inner1.cancelled())
        self.assertTrue(inner2.cancelled())
        child1.set_result(1)
        child2.set_result(2)
        test_utils.run_briefly(self.loop) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_tasks.py

示例10: test_maxsize_release

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def test_maxsize_release(self):
        pool = Pool(url="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(asyncio.TimeoutError):
                shielded_fut = asyncio.shield(c3)
                await asyncio.wait_for(shielded_fut, 0.1)
            await pool.release(c2)
            c3 = await c3
            self.assertEqual(c2, c3)
            c1.conn.close()
            c2.conn.close()
            c3.conn.close()

        self.loop.run_until_complete(go()) 
開發者ID:davebshow,項目名稱:gremlinclient,代碼行數:25,代碼來源:test_asyncio_PEP492.py

示例11: test_maxsize_release

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def test_maxsize_release(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="stephen",
                    password="password",
                    future_class=Future)

        @asyncio.coroutine
        def go():
            c1 = yield from pool.acquire()
            c2 = yield from pool.acquire()
            c3 = pool.acquire()
            self.assertIsInstance(c3, Future)
            with self.assertRaises(asyncio.TimeoutError):
                shielded_fut = asyncio.shield(c3)
                yield from asyncio.wait_for(shielded_fut, 0.1)
            yield from pool.release(c2)
            c3 = yield from c3
            self.assertEqual(c2, c3)
            c1.conn.close()
            c2.conn.close()
            c3.conn.close()

        self.loop.run_until_complete(go()) 
開發者ID:davebshow,項目名稱:gremlinclient,代碼行數:26,代碼來源:test_asyncio.py

示例12: get_tx_events

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def get_tx_events(self):

        org = 'org1.example.com'
        peer = self.client.get_peer('peer0.' + org)

        org_admin = self.client.get_user(org, 'Admin')
        channel = self.client.get_channel(self.channel_name)
        channel_event_hub = channel.newChannelEventHub(peer, org_admin)
        stream = channel_event_hub.connect(start='oldest',
                                           stop='newest', filtered=False)

        self.txs = {}
        channel_event_hub.registerTxEvent('all', onEvent=self.onTxEvent)

        try:
            await shield(stream)
        except Exception:
            pass

        channel_event_hub.disconnect()

        self.assertEqual(len(self.txs['all']), 4) 
開發者ID:hyperledger,項目名稱:fabric-sdk-py,代碼行數:24,代碼來源:e2e_test.py

示例13: _fail

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def _fail(self, e: Exception, clean_close: bool = True) -> None:
        if self.close_task is None:
            self.close_task = asyncio.shield(
                asyncio.ensure_future(self._close_coro(e, clean_close=clean_close))
            ) 
開發者ID:graphql-python,項目名稱:gql,代碼行數:7,代碼來源:websockets.py

示例14: disconnected

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def disconnected(self):
        """
        Future that resolves when the connection to Telegram
        ends, either by user action or in the background.

        Note that it may resolve in either a ``ConnectionError``
        or any other unexpected error that could not be handled.
        """
        return asyncio.shield(self._disconnected, loop=self._loop)

    # Private methods 
開發者ID:LonamiWebs,項目名稱:Telethon,代碼行數:13,代碼來源:mtprotosender.py

示例15: _scheduled_task

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import shield [as 別名]
def _scheduled_task(self, data: TaskData) -> None:
        """Await the `data.callback` coroutine after waiting for `data.wait_time` seconds."""
        try:
            log.trace(f"Waiting {data.wait_time} seconds before awaiting callback.")
            await asyncio.sleep(data.wait_time)

            # Use asyncio.shield to prevent callback from cancelling itself.
            # The parent task (_scheduled_task) will still get cancelled.
            log.trace("Done waiting; now awaiting the callback.")
            await asyncio.shield(data.callback)
        finally:
            if inspect.iscoroutine(data.callback):
                log.trace("Explicitly closing coroutine.")
                data.callback.close() 
開發者ID:python-discord,項目名稱:bot,代碼行數:16,代碼來源:help_channels.py


注:本文中的asyncio.shield方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。