[
https://issues.apache.org/jira/browse/TINKERPOP-2774?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18131226#comment-18131226
]
ASF GitHub Bot commented on TINKERPOP-2774:
-------------------------------------------
masterhugo commented on code in PR #3527:
URL: https://github.com/apache/tinkerpop/pull/3527#discussion_r4365250504
##########
gremlin-python/src/main/python/gremlin_python/driver/async_resultset.py:
##########
@@ -0,0 +1,148 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+import asyncio
+import queue
+
+from gremlin_python.driver.resultset import ResultSet
+
+__author__ = 'Apache TinkerPop ([email protected])'
+
+# Internal sentinel used to signal that the stream is exhausted.
+_EXHAUSTED = object()
+
+
+class AsyncResultSet(ResultSet):
+ """Result set backed by an :class:`asyncio.Queue` and an
+ :class:`asyncio.Future`.
+
+ Consumption methods (:meth:`one`, :meth:`all`) are coroutines. The class
+ also supports ``async for`` iteration.
+
+ The ``future`` attribute **must** be set to a resolved
+ :class:`asyncio.Future` or a running :class:`asyncio.Task` by the owning
+ :class:`~gremlin_python.driver.async_connection.AsyncConnection` before
+ any consumption method is called.
+ """
+
+ def __init__(self, stream, request_id):
+ # stream must be an asyncio.Queue
+ super().__init__(stream, request_id)
+ self._future = None
+ # Cache individual items already unpacked from a multi-item chunk so
+ # that successive one() calls each return exactly one result.
+ self._item_cache = queue.Queue()
+
+ # ------------------------------------------------------------------
+ # future property (asyncio.Future / asyncio.Task)
+ # ------------------------------------------------------------------
+
+ @property
+ def future(self):
+ return self._future
+
+ @future.setter
+ def future(self, value):
+ self._future = value
+
+ # ------------------------------------------------------------------
+ # Disable the synchronous interface
+ # ------------------------------------------------------------------
+
+ def __iter__(self):
+ raise NotImplementedError(
+ "AsyncResultSet supports only async iteration; use 'async for'"
+ )
+
+ def __next__(self):
+ raise NotImplementedError(
+ "AsyncResultSet supports only async iteration; use 'async for'"
+ )
+
+ # ------------------------------------------------------------------
+ # Async iteration protocol
+ # ------------------------------------------------------------------
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ result = await self.one()
+ if result is _EXHAUSTED:
+ raise StopAsyncIteration
+ return result
+
+ # ------------------------------------------------------------------
+ # Consumption
+ # ------------------------------------------------------------------
+
+ async def one(self):
+ """Return the next individual result or the ``_EXHAUSTED`` sentinel.
+
+ Each server response chunk contains a list of items. ``one()``
+ unpacks those lists so that each call returns exactly one item,
Review Comment:
Clarified this in the docstring (and made sure all() drains any cached items
first if one() was called before).
> Full async/await support in gremlin-python
> ------------------------------------------
>
> Key: TINKERPOP-2774
> URL: https://issues.apache.org/jira/browse/TINKERPOP-2774
> Project: TinkerPop
> Issue Type: Improvement
> Components: python
> Affects Versions: 3.5.3
> Reporter: Ken Hu
> Priority: Minor
>
> Recently, there have been multiple instances of users asking about issues
> with event loops and aiohttp. This discussion nearly always ends up with the
> user requesting for async/await support in gremlin-python. This means full
> compatibility with asyncio and not needing to use threads.
> Async/await support is also a feature that those using newer, asynchronous
> web frameworks are asking for as gremlin-python can be a performance
> bottleneck due to its lack of support for async/await.
> My intention is that we can use this issue as a means of gauging the interest
> from the community for this feature. If you are interested in having
> gremlin-python support async/await then please leave a comment below.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)