Skip to content

Commit 29357fe

Browse files
author
Thinh Nguyen
committed
add mechanism to return populate's success count
1 parent 7747796 commit 29357fe

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

datajoint/autopopulate.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def populate(
160160
display_progress=False,
161161
processes=1,
162162
make_kwargs=None,
163+
return_success_count=False
163164
):
164165
"""
165166
``table.populate()`` calls ``table.make(key)`` for every primary key in
@@ -217,6 +218,7 @@ def handler(signum, frame):
217218
processes = min(_ for _ in (processes, nkeys, mp.cpu_count()) if _)
218219

219220
error_list = []
221+
success_list = []
220222
populate_kwargs = dict(
221223
suppress_errors=suppress_errors,
222224
return_exception_objects=return_exception_objects,
@@ -227,9 +229,12 @@ def handler(signum, frame):
227229
for key in (
228230
tqdm(keys, desc=self.__class__.__name__) if display_progress else keys
229231
):
230-
error = self._populate1(key, jobs, **populate_kwargs)
231-
if error is not None:
232-
error_list.append(error)
232+
status = self._populate1(key, jobs, **populate_kwargs)
233+
if status is not None:
234+
if isinstance(status, tuple):
235+
error_list.append(status)
236+
elif status:
237+
success_list.append(1)
233238
else:
234239
# spawn multiple processes
235240
self.connection.close() # disconnect parent process from MySQL server
@@ -241,9 +246,12 @@ def handler(signum, frame):
241246
if display_progress
242247
else contextlib.nullcontext()
243248
) as progress_bar:
244-
for error in pool.imap(_call_populate1, keys, chunksize=1):
245-
if error is not None:
246-
error_list.append(error)
249+
for status in pool.imap(_call_populate1, keys, chunksize=1):
250+
if status is not None:
251+
if isinstance(status, tuple):
252+
error_list.append(status)
253+
elif status:
254+
success_list.append(1)
247255
if display_progress:
248256
progress_bar.update()
249257
self.connection.connect() # reconnect parent process to MySQL server
@@ -252,8 +260,12 @@ def handler(signum, frame):
252260
if reserve_jobs:
253261
signal.signal(signal.SIGTERM, old_handler)
254262

263+
if suppress_errors and return_success_count:
264+
return sum(success_list), error_list
255265
if suppress_errors:
256266
return error_list
267+
if return_success_count:
268+
return sum(success_list)
257269

258270
def _populate1(
259271
self, key, jobs, suppress_errors, return_exception_objects, make_kwargs=None
@@ -311,6 +323,7 @@ def _populate1(
311323
)
312324
if jobs is not None:
313325
jobs.complete(self.target.table_name, self._job_key(key))
326+
return True
314327
finally:
315328
self.__class__._allow_insert = False
316329

0 commit comments

Comments
 (0)