Skip to content

Commit 9221213

Browse files
committed
[IMP] util/pg: add parallel_execute docstring
Part-of: #49
1 parent 5519ad3 commit 9221213

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/util/pg.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,6 @@ def _parallel_execute_serial(cr, queries, logger=_logger):
8484
if ThreadPoolExecutor is not None:
8585

8686
def _parallel_execute_threaded(cr, queries, logger=_logger):
87-
"""
88-
Execute queries in parallel.
89-
90-
Use a maximum of 8 workers (but not more than the number of CPUs)
91-
Side effect: the given cursor is commited.
92-
As example, on `**REDACTED**` (using 8 workers), the following gains are:
93-
+---------------------------------------------+-------------+-------------+
94-
| File | Sequential | Parallel |
95-
+---------------------------------------------+-------------+-------------+
96-
| base/saas~12.5.1.3/pre-20-models.py | ~8 minutes | ~2 minutes |
97-
| mail/saas~12.5.1.0/pre-migrate.py | ~10 minutes | ~4 minutes |
98-
| mass_mailing/saas~12.5.2.0/pre-10-models.py | ~40 minutes | ~18 minutes |
99-
+---------------------------------------------+-------------+-------------+
100-
"""
10187
if not queries:
10288
return None
10389

@@ -152,6 +138,32 @@ def execute(query):
152138

153139

154140
def parallel_execute(cr, queries, logger=_logger):
141+
"""
142+
Execute queries in parallel.
143+
144+
.. example::
145+
.. code-block:: python
146+
147+
util.parallel_execute(cr, [util.format_query(cr, "REINDEX TABLE {}", t) for t in tables])
148+
149+
.. tip::
150+
If looking to speedup a single query, see :func:`~odoo.upgrade.util.pg.explode_execute`.
151+
152+
:param list(str) queries: list of queries to execute concurrently
153+
:param `~logging.Logger` logger: logger used to report the progress
154+
:return: the sum of `cr.rowcount` for each query run
155+
:rtype: int
156+
157+
.. warning::
158+
- Due to the nature of `cr.rowcount`, the return value of this function may represent an
159+
underestimate of the real number of affected records. For instance, when some records
160+
are deleted/updated as a result of an `ondelete` clause, they won't be taken into account.
161+
162+
- As a side effect, the cursor will be committed.
163+
164+
.. note::
165+
If a concurrency issue occurs, the *failing* queries will be retried sequentially.
166+
"""
155167
parallel_execute_impl = (
156168
_parallel_execute_serial
157169
if getattr(threading.current_thread(), "testing", False)
@@ -330,6 +342,7 @@ def explode_execute(cr, query, table, alias=None, bucket_size=10000, logger=_log
330342
It's up to the caller to ensure the queries do not update the same records in
331343
different buckets. It is advised to never use this function for `DELETE` queries on
332344
tables with self references due to the potential `ON DELETE` effects.
345+
For more details see :func:`~odoo.upgrade.util.pg.parallel_execute`.
333346
"""
334347
return parallel_execute(
335348
cr,

0 commit comments

Comments
 (0)