@@ -84,20 +84,6 @@ def _parallel_execute_serial(cr, queries, logger=_logger):
84
84
if ThreadPoolExecutor is not None :
85
85
86
86
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
- """
101
87
if not queries :
102
88
return None
103
89
@@ -152,6 +138,32 @@ def execute(query):
152
138
153
139
154
140
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
+ """
155
167
parallel_execute_impl = (
156
168
_parallel_execute_serial
157
169
if getattr (threading .current_thread (), "testing" , False )
@@ -330,6 +342,7 @@ def explode_execute(cr, query, table, alias=None, bucket_size=10000, logger=_log
330
342
It's up to the caller to ensure the queries do not update the same records in
331
343
different buckets. It is advised to never use this function for `DELETE` queries on
332
344
tables with self references due to the potential `ON DELETE` effects.
345
+ For more details see :func:`~odoo.upgrade.util.pg.parallel_execute`.
333
346
"""
334
347
return parallel_execute (
335
348
cr ,
0 commit comments