* [PATCH] lib/oe/package_manager: Add utils function for multiprocess execution
@ 2014-08-21 20:46 Richard Purdie
2014-08-22 0:57 ` Christopher Larson
0 siblings, 1 reply; 3+ messages in thread
From: Richard Purdie @ 2014-08-21 20:46 UTC (permalink / raw)
To: openembedded-core
Our usage of multitprocessing is problematic. In particular, there is a bug
in python 2.7 multiprocessing where signals are not handled until command
completion instead of immediately.
This factors the multiprocess code into a function which is enhanced with
a workaround to ensure immediate signal handling and also better SIGINT
handling which should happen in the parent, not the children to ensure
clean exits. The workaround for the signals is being added to the core
bb.utils function so it can benefit all users.
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
diff --git a/meta/lib/oe/package_manager.py b/meta/lib/oe/package_manager.py
index 8be3d41..f8fc3c2 100644
--- a/meta/lib/oe/package_manager.py
+++ b/meta/lib/oe/package_manager.py
@@ -7,6 +7,7 @@ import multiprocessing
import re
import bb
import tempfile
+import oe.utils
# this can be used by all PM backends to create the index files in parallel
@@ -116,16 +117,7 @@ class RpmIndexer(Indexer):
bb.note("There are no packages in %s" % self.deploy_dir)
return
- nproc = multiprocessing.cpu_count()
- pool = bb.utils.multiprocessingpool(nproc)
- results = list(pool.imap(create_index, index_cmds))
- pool.close()
- pool.join()
-
- for result in results:
- if result is not None:
- return(result)
-
+ oe.utils.multiprocess_exec(index_cmds, create_index)
class OpkgIndexer(Indexer):
def write_index(self):
@@ -161,15 +153,7 @@ class OpkgIndexer(Indexer):
bb.note("There are no packages in %s!" % self.deploy_dir)
return
- nproc = multiprocessing.cpu_count()
- pool = bb.utils.multiprocessingpool(nproc)
- results = list(pool.imap(create_index, index_cmds))
- pool.close()
- pool.join()
-
- for result in results:
- if result is not None:
- return(result)
+ oe.utils.multiprocess_exec(index_cmds, create_index)
class DpkgIndexer(Indexer):
@@ -210,15 +194,7 @@ class DpkgIndexer(Indexer):
bb.note("There are no packages in %s" % self.deploy_dir)
return
- nproc = multiprocessing.cpu_count()
- pool = bb.utils.multiprocessingpool(nproc)
- results = list(pool.imap(create_index, index_cmds))
- pool.close()
- pool.join()
-
- for result in results:
- if result is not None:
- return(result)
+ oe.utils.multiprocess_exec(index_cmds, create_index)
class PkgsList(object):
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index 0a1d108..92e21a4 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -151,3 +151,32 @@ def execute_pre_post_process(d, cmds):
if cmd != '':
bb.note("Executing %s ..." % cmd)
bb.build.exec_func(cmd, d)
+
+def multiprocess_exec(commands, function):
+ import signal
+ import multiprocessing
+
+ if not commands:
+ return []
+
+ def init_worker():
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
+
+ nproc = min(multiprocessing.cpu_count(), len(commands))
+ pool = bb.utils.multiprocessingpool(nproc, init_worker)
+ imap = pool.imap(function, commands)
+
+ try:
+ results = list(imap)
+ pool.close()
+ pool.join()
+ results = []
+ for result in results:
+ if result is not None:
+ results.append(result)
+ return results
+
+ except KeyboardInterrupt:
+ pool.terminate()
+ pool.join()
+ raise
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] lib/oe/package_manager: Add utils function for multiprocess execution
2014-08-21 20:46 [PATCH] lib/oe/package_manager: Add utils function for multiprocess execution Richard Purdie
@ 2014-08-22 0:57 ` Christopher Larson
2014-08-23 11:04 ` Richard Purdie
0 siblings, 1 reply; 3+ messages in thread
From: Christopher Larson @ 2014-08-22 0:57 UTC (permalink / raw)
To: Richard Purdie; +Cc: openembedded-core
[-- Attachment #1: Type: text/plain, Size: 692 bytes --]
On Thu, Aug 21, 2014 at 1:46 PM, Richard Purdie <
richard.purdie@linuxfoundation.org> wrote:
> Our usage of multitprocessing is problematic. In particular, there is a bug
> in python 2.7 multiprocessing where signals are not handled until command
> completion instead of immediately.
This looks good, but the subject is misleading, in my opinion:
"lib/oe/package_manager:
Add utils function for multiprocess execution". It should probably either
mention both utils and package_manager, or be split into two commits.
--
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
[-- Attachment #2: Type: text/html, Size: 1096 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] lib/oe/package_manager: Add utils function for multiprocess execution
2014-08-22 0:57 ` Christopher Larson
@ 2014-08-23 11:04 ` Richard Purdie
0 siblings, 0 replies; 3+ messages in thread
From: Richard Purdie @ 2014-08-23 11:04 UTC (permalink / raw)
To: Christopher Larson; +Cc: openembedded-core
On Thu, 2014-08-21 at 17:57 -0700, Christopher Larson wrote:
> On Thu, Aug 21, 2014 at 1:46 PM, Richard Purdie
> <richard.purdie@linuxfoundation.org> wrote:
> Our usage of multitprocessing is problematic. In particular,
> there is a bug
> in python 2.7 multiprocessing where signals are not handled
> until command
> completion instead of immediately.
>
> This looks good, but the subject is misleading, in my opinion:
> "lib/oe/package_manager: Add utils function for multiprocess
> execution". It should probably either mention both utils and
> package_manager, or be split into two commits.
Good point, I tweaked before merging, thanks. It was the (bad) result of
rearranging a few commits.
Cheers,
Richard
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-08-23 11:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-21 20:46 [PATCH] lib/oe/package_manager: Add utils function for multiprocess execution Richard Purdie
2014-08-22 0:57 ` Christopher Larson
2014-08-23 11:04 ` Richard Purdie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox