Gremlin Python in Web Application

浪子不回头ぞ 提交于 2020-01-04 08:15:11

问题


I have a python flask web app, which is querying a Janus graph DB using gremlin_python. One basic question is the correct way to initialize the graph traversal object.

  1. Can I initialize my traversal g = traversal().withRemote(DriverRemoteConnection(...) and persist the traversal variable g across requests? (All requests are against the same graph. I tried this and started getting tornado.iostream.StreamClosedError intermittently.
  2. Second option is the create a traversal per request. I do not understand the gremlin python architecture well enough; is there a substantial overhead of doing this per request?

Thank You


回答1:


Gremlin Python is a Gremlin Language Variant implementation see http://tinkerpop.apache.org/docs/current/tutorials/gremlin-language-variants/.

As such it does rely on a GremlinServer to execute traversals.

The assignment:

g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

will open a websocket connection to the server. There is no such thing as "persisting" by keeping a copy of that connection. The persistence mechanisms all happen on the server side. I found this out the hard way when I tried out the following code:

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
import os

g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

# test loading a graph
def test_loadGraph():
   # make the local file accessible to the server
   airRoutesPath=os.path.abspath("air-routes-small.xml")
   # drop the existing content of the graph
   g.V().drop().iterate()
   # read the content from the air routes example
   g.io(airRoutesPath).read().iterate()
   vCount=g.V().count().next()
   assert vCount==1000

The above code works and loads the air-routes example. But it breaks all other tests since now the default modern graph of the used server in the tutorial mentioned below is gone!

You can open multiple websocket connections to the server but they all share the same "state" of the underlying graph. The bad news is that it is not easy to influence this state via APIs which is a deliberate decision of the current architecture. Currently there is a lot of configuring yaml and propertries files and starting and stopping servers involved.

My Improvement proposal https://issues.apache.org/jira/projects/TINKERPOP/issues/TINKERPOP-2294?filter=allopenissues is based on the frustration this approach brings with it.

Especially I share your "I do not understand the gremlin python architecture well enough". IMHO this is not because you or i have a problem in understanding it but because it is not explained well enough. Especially examples are missing. This is why I started: http://wiki.bitplan.com/index.php/Gremlin_python - a tutorial that is intended to make getting started with gremlin python less painful.



来源:https://stackoverflow.com/questions/55876563/gremlin-python-in-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!