[ 
https://issues.apache.org/jira/browse/TINKERPOP-2774?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146766#comment-18146766
 ] 

ASF GitHub Bot commented on TINKERPOP-2774:
-------------------------------------------

masterhugo commented on code in PR #3527:
URL: https://github.com/apache/tinkerpop/pull/3527#discussion_r4615298901


##########
gremlin-python/src/main/python/gremlin_python/driver/async_protocol.py:
##########
@@ -0,0 +1,142 @@
+#
+# 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.
+#
+"""Experimental API. This API may have breaking changes or be removed in a 
subsequent minor release.
+
+Asynchronous Gremlin Server WebSocket subprotocol implementation.
+"""
+import asyncio
+import base64
+import logging
+
+from gremlin_python.driver import request
+from gremlin_python.driver.protocol import (
+    ConfigurationError,
+    GremlinServerError,
+    GremlinServerWSProtocol,
+)
+
+log = logging.getLogger("gremlinpython")
+
+class AsyncGremlinServerWSProtocol(GremlinServerWSProtocol):
+    """Experimental API. This API may have breaking changes or be removed in a 
subsequent minor release.
+
+    Async version of GremlinServerWSProtocol.
+
+    Both write() and data_received() are coroutines that must be awaited.
+    The transport assigned via connection_made() must be an
+    AsyncAiohttpWSTransport (i.e. its write and read methods must be
+    coroutines).
+    """
+
+    async def write(self, request_id, request_message):
+        """Experimental API. This API may have breaking changes or be removed 
in a subsequent minor release.
+
+        Serialize *request_message* and send it over the WebSocket.
+        """
+        message = self._message_serializer.serialize_message(
+            request_id, request_message
+        )
+        await self._transport.write(message)
+
+    async def data_received(self, message, results_dict):
+        """Experimental API. This API may have breaking changes or be removed 
in a subsequent minor release.
+
+        Deserialize a raw WebSocket frame and dispatch results.
+
+        Deserialization is offloaded to the default executor so that the event
+        loop is not blocked by CPU-bound work.
+
+        Returns the Gremlin Server response status code (200, 204, or 206).
+        Raises GremlinServerError for error status codes.
+        """
+        if message is None:
+            log.error("Received empty message from server.")
+            raise GremlinServerError(
+                {
+                    "code": 500,
+                    "message": "Server disconnected - please try to reconnect",
+                    "attributes": {},
+                }
+            )
+
+        loop = asyncio.get_running_loop()
+        message = await loop.run_in_executor(
+            None, self._message_serializer.deserialize_message, message
+        )
+
+        request_id = message["requestId"]
+        result_set = results_dict.get(request_id)

Review Comment:
   Updated data_received() to fail with a RuntimeError when the requestId does 
not match an active entry in results_dict (so the connection is invalidated and 
returned cleanly), and added unit tests covering mismatched IDs for 200, 204, 
206, and 407.





> 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)

Reply via email to