amazon-neptunegremlinpythonrasa-xuvloopnest-asyncio

Running into uvloop issues with Database queries from Rasa-X?


I'm trying to make a simple query to my amazon neptune database, from Rasa-x.

Here is the code from my actions.py:

class ActionQueryDietary(Action):
    def name(self) -> Text:
        return "action_query_dietary"

    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        available = False
        restaurant = "XXXX"
        dietaryQuestion=tracker.get_slot('dietaryQuestion')
        g, remoteConn = kb.openConnection()

        dietary = kb.getDietary(g, restaurant, dietaryQuestion)

        if(dietary=="Yes"):
            available = True
        
        if(available==True):
            dispatcher.utter_message(text="According to our knowledge base, {} is on the menu").format(dietaryQuestion)
        else:
            dispatcher.utter_message(text="Sorry, according to our knowledge base, we don't have this option on the menu")
        kb.closeConnection(remoteConn)
        return []

and here is the code from knowledgebase.py:

def getDietary(g, restaurant, dietary):
    properties = queryRestaurantProperties(g, restaurant)
    result = properties[dietary]
    print(result)
    return result

but any query to the knowledgebase results in this error:

2020-10-22T18:01:22.347345241Z   File "/opt/venv/lib/python3.7/site-packages/sanic/app.py", line 973, in handle_request
2020-10-22T18:01:22.347351643Z     response = await response
2020-10-22T18:01:22.347357446Z   File "/app/rasa_sdk/endpoint.py", line 102, in webhook
2020-10-22T18:01:22.347363522Z     result = await executor.run(action_call)
2020-10-22T18:01:22.347369398Z   File "/app/rasa_sdk/executor.py", line 392, in run
2020-10-22T18:01:22.347375473Z     events = action(dispatcher, tracker, domain)
2020-10-22T18:01:22.347381348Z   File "/app/actions/actions.py", line 33, in run
2020-10-22T18:01:22.347387063Z     dietary = kb.getDietary(g, restaurant, dietaryQuestion)
2020-10-22T18:01:22.347392835Z   File "/app/actions/knowledgebase.py", line 117, in getDietary
2020-10-22T18:01:22.347399112Z     properties = queryRestaurantProperties(g, restaurant)
2020-10-22T18:01:22.347405111Z   File "/app/actions/knowledgebase.py", line 86, in queryRestaurantProperties
2020-10-22T18:01:22.347411869Z     result = g.V().has('name', restaurant).valueMap().next()
2020-10-22T18:01:22.347418048Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 89, in next
2020-10-22T18:01:22.347424293Z     return self.__next__()
2020-10-22T18:01:22.347430069Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 48, in __next__
2020-10-22T18:01:22.347436333Z     self.traversal_strategies.apply_strategies(self)
2020-10-22T18:01:22.347441940Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 573, in apply_strategies
2020-10-22T18:01:22.347447667Z     traversal_strategy.apply(traversal)
2020-10-22T18:01:22.347453031Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/remote_connection.py", line 149, in apply
2020-10-22T18:01:22.347459352Z     remote_traversal = self.remote_connection.submit(traversal.bytecode)
2020-10-22T18:01:22.347465069Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 55, in submit
2020-10-22T18:01:22.347486749Z     result_set = self._client.submit(bytecode)
2020-10-22T18:01:22.347493788Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/client.py", line 127, in submit
2020-10-22T18:01:22.347499424Z     return self.submitAsync(message, bindings=bindings).result()
2020-10-22T18:01:22.347505093Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/client.py", line 146, in submitAsync
2020-10-22T18:01:22.347511092Z     return conn.write(message)
2020-10-22T18:01:22.347516610Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/connection.py", line 55, in write
2020-10-22T18:01:22.347522673Z     self.connect()
2020-10-22T18:01:22.347529987Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/connection.py", line 45, in connect
2020-10-22T18:01:22.347536097Z     self._transport.connect(self._url, self._headers)
2020-10-22T18:01:22.347542222Z   File "/opt/venv/lib/python3.7/site-packages/gremlin_python/driver/tornado/transport.py", line 36, in connect
2020-10-22T18:01:22.347547822Z     lambda: websocket.websocket_connect(url))
2020-10-22T18:01:22.347553477Z   File "/opt/venv/lib/python3.7/site-packages/tornado/ioloop.py", line 571, in run_sync
2020-10-22T18:01:22.347559295Z     self.start()
2020-10-22T18:01:22.347564864Z   File "/opt/venv/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 132, in start
2020-10-22T18:01:22.347570978Z     self.asyncio_loop.run_forever()
2020-10-22T18:01:22.347576693Z   File "uvloop/loop.pyx", line 1351, in uvloop.loop.Loop.run_forever
2020-10-22T18:01:22.347582342Z   File "uvloop/loop.pyx", line 484, in uvloop.loop.Loop._run
2020-10-22T18:01:22.347588222Z RuntimeError: Cannot run the event loop while another loop is running

I tried using nest_asyncio.apply, but that resulted in this error:

ValueError: Can't patch loop of type <class 'uvloop.Loop'>

which according to the docs is just a rule.

So I don't really know how to proceed?


Solution

  • Adding my comment above as an answer. In some cases it is necessary to downlevel the version of Tornado being used. There are some issues that you can sometimes run into if the event loop is already running when someone else tries to create it. For now, down leveling to Tornado 4.5.1 with Gremlin Python should resolve any issues.