[
https://issues.apache.org/jira/browse/TINKERPOP-2774?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146770#comment-18146770
]
ASF GitHub Bot commented on TINKERPOP-2774:
-------------------------------------------
masterhugo commented on code in PR #3527:
URL: https://github.com/apache/tinkerpop/pull/3527#discussion_r4615314523
##########
gremlin-python/src/main/python/gremlin_python/driver/async_connection.py:
##########
@@ -0,0 +1,220 @@
+#
+# 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.
+
+Native-async WebSocket connection managed by AsyncClient.
+"""
+import asyncio
+import logging
+import uuid
+
+from gremlin_python.driver import useragent
+from gremlin_python.driver.async_resultset import AsyncResultSet
+
+log = logging.getLogger("gremlinpython")
+
+
+class AsyncConnection:
+ """Experimental API. This API may have breaking changes or be removed in a
subsequent minor release.
+
+ Native-async WebSocket connection with keepalive.
+
+ Features:
+
+ * Ping keepalive: a background asyncio.Task sends a WebSocket
+ ping every *ping_interval* seconds to prevent the server or intermediate
+ proxies from closing idle connections.
+
+ Parameters
+ ----------
+ url:
+ WebSocket endpoint.
+ traversal_source:
+ Gremlin traversal source name (e.g. "g").
+ protocol:
+ An AsyncGremlinServerWSProtocol instance.
+ transport_factory:
+ Zero-argument callable returning an AsyncAiohttpWSTransport.
+ pool:
+ The asyncio.Queue that owns this connection.
+ headers:
+ Extra HTTP headers sent during the WebSocket handshake.
+ enable_user_agent_on_connect:
+ Whether to send the driver user-agent header.
+ ping_interval:
+ Seconds between WebSocket ping frames (default 60, 0 to disable).
+ """
+
+ def __init__(self, url, traversal_source, protocol, transport_factory,
+ pool, headers=None, enable_user_agent_on_connect=True,
+ ping_interval=60):
+ self._url = url
+ self._headers = headers
+ self._traversal_source = traversal_source
+ self._protocol = protocol
+ self._transport_factory = transport_factory
+ self._transport = None
+ self._pool = pool
+ self._results = {}
+ self._inited = False
+ if enable_user_agent_on_connect:
+ self._add_header(useragent.userAgentHeader, useragent.userAgent)
+ self._ping_interval = ping_interval
+ self._ping_task = None
+ self._receive_task = None
+
+ def _add_header(self, key, value):
+ if self._headers is None:
+ self._headers = dict()
+ if key.lower() not in [k.lower() for k in self._headers.keys()]:
+ self._headers[key] = value
+
+ # ------------------------------------------------------------------
+ # Lifecycle
+ # ------------------------------------------------------------------
+
+ async def connect(self):
+ """Experimental API. This API may have breaking changes or be removed
in a subsequent minor release.
+
+ Open (or re-open) the WebSocket connection.
+ """
+ if self._ping_task is not None:
+ self._ping_task.cancel()
+ self._ping_task = None
+ if self._transport is not None:
+ await self._transport.close()
+ self._transport = self._transport_factory()
+ await self._transport.connect(self._url, self._headers)
+ self._protocol.connection_made(self._transport)
+ self._inited = True
+ if self._ping_interval > 0:
+ self._ping_task = asyncio.create_task(self._ping_forever())
+
+ async def close(self):
+ """Experimental API. This API may have breaking changes or be removed
in a subsequent minor release.
+
+ Close the connection and stop its background tasks.
+ """
+ await asyncio.shield(self._close())
+
+ async def _close(self):
+ if self._ping_task is not None:
+ self._ping_task.cancel()
+ self._ping_task = None
+ if self._receive_task is not None:
+ self._receive_task.cancel()
+ await asyncio.gather(self._receive_task, return_exceptions=True)
+ try:
+ if self._transport is not None:
+ await self._transport.close()
+ finally:
+ self._inited = False
+ self._results.clear()
+
+ # ------------------------------------------------------------------
+ # Keepalive
+ # ------------------------------------------------------------------
+
+ async def _ping_forever(self):
+ """Send periodic WebSocket ping frames until cancelled or the
transport dies."""
+ while True:
+ try:
+ await asyncio.sleep(self._ping_interval)
+ await self._transport.ping()
Review Comment:
Removed ping_interval, _ping_forever(), and the custom ping() method, and
documented heartbeat forwarding through aiohttp instead, plus unit tests
verifying transport compatibility without ping().
> 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)