mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-16 15:47:58 +00:00
add function for parallel execution of SQL scripts
This commit is contained in:
@@ -11,6 +11,7 @@ from typing import Set, Dict, Any
|
|||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
from nominatim.db.connection import Connection
|
from nominatim.db.connection import Connection
|
||||||
|
from nominatim.db.async_connection import WorkerPool
|
||||||
from nominatim.config import Configuration
|
from nominatim.config import Configuration
|
||||||
|
|
||||||
def _get_partitions(conn: Connection) -> Set[int]:
|
def _get_partitions(conn: Connection) -> Set[int]:
|
||||||
@@ -96,3 +97,21 @@ class SQLPreprocessor:
|
|||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def run_parallel_sql_file(self, dsn: str, name: str, num_threads: int = 1,
|
||||||
|
**kwargs: Any) -> None:
|
||||||
|
""" Execure the given SQL files using parallel asynchronous connections.
|
||||||
|
The keyword arguments may supply additional parameters for
|
||||||
|
preprocessing.
|
||||||
|
|
||||||
|
After preprocessing the SQL code is cut at lines containing only
|
||||||
|
'---'. Each chunk is sent to one of the `num_threads` workers.
|
||||||
|
"""
|
||||||
|
sql = self.env.get_template(name).render(**kwargs)
|
||||||
|
|
||||||
|
parts = sql.split('\n---\n')
|
||||||
|
|
||||||
|
with WorkerPool(dsn, num_threads) as pool:
|
||||||
|
for part in parts:
|
||||||
|
pool.next_free_worker().perform(part)
|
||||||
|
|||||||
Reference in New Issue
Block a user