]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/api.py
Merge pull request #2937 from lonvia/python-server-stub
[nominatim.git] / nominatim / api.py
index f5e942d1f3377936de57bc9e8afc5d87a533d579..10cca5330d6ae11fa1699933c27e61ecac330e21 100644 (file)
@@ -7,12 +7,14 @@
 """
 Implementation of classes for API access via libraries.
 """
-from typing import Mapping, Optional
+from typing import Mapping, Optional, cast, Any
 import asyncio
 from pathlib import Path
 
+from sqlalchemy import text, event
 from sqlalchemy.engine.url import URL
 from sqlalchemy.ext.asyncio import create_async_engine
+import asyncpg
 
 from nominatim.config import Configuration
 from nominatim.apicmd.status import get_status, StatusResult
@@ -33,9 +35,36 @@ class NominatimAPIAsync:
                    host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
                    query={k: v for k, v in dsn.items()
                           if k not in ('user', 'password', 'dbname', 'host', 'port')})
-        self.engine = create_async_engine(dburl,
-                                          connect_args={"server_settings": {"jit": "off"}},
-                                          future=True)
+        self.engine = create_async_engine(
+                         dburl, future=True,
+                         connect_args={'server_settings': {
+                            'DateStyle': 'sql,european',
+                            'max_parallel_workers_per_gather': '0'
+                         }})
+        asyncio.get_event_loop().run_until_complete(self._query_server_version())
+        asyncio.get_event_loop().run_until_complete(self.close())
+
+        if self.server_version >= 110000:
+            @event.listens_for(self.engine.sync_engine, "connect") # type: ignore[misc]
+            def _on_connect(dbapi_con: Any, _: Any) -> None:
+                cursor = dbapi_con.cursor()
+                cursor.execute("SET jit_above_cost TO '-1'")
+
+
+    async def _query_server_version(self) -> None:
+        try:
+            async with self.engine.begin() as conn:
+                result = await conn.scalar(text('SHOW server_version_num'))
+                self.server_version = int(cast(str, result))
+        except asyncpg.PostgresError:
+            self.server_version = 0
+
+    async def close(self) -> None:
+        """ Close all active connections to the database. The NominatimAPIAsync
+            object remains usable after closing. If a new API functions is
+            called, new connections are created.
+        """
+        await self.engine.dispose()
 
 
     async def status(self) -> StatusResult:
@@ -53,6 +82,14 @@ class NominatimAPI:
         self.async_api = NominatimAPIAsync(project_dir, environ)
 
 
+    def close(self) -> None:
+        """ Close all active connections to the database. The NominatimAPIAsync
+            object remains usable after closing. If a new API functions is
+            called, new connections are created.
+        """
+        asyncio.get_event_loop().run_until_complete(self.async_api.close())
+
+
     def status(self) -> StatusResult:
         """ Return the status of the database.
         """