Merge "pmu mirror creation ansible error fix"
[yardstick.git] / yardstick / common / process.py
1 # Copyright (c) 2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15 import multiprocessing
16
17 import os
18
19 LOG = logging.getLogger(__name__)
20
21
22 def check_if_process_failed(proc, timeout=1):
23     if proc is not None:
24         proc.join(timeout)
25         # Only abort if the process aborted
26         if proc.exitcode is not None and proc.exitcode > 0:
27             raise RuntimeError("{} exited with status {}".format(proc.name, proc.exitcode))
28
29
30 def terminate_children(timeout=3):
31     current_proccess = multiprocessing.current_process()
32     active_children = multiprocessing.active_children()
33     if not active_children:
34         LOG.debug("no children to terminate")
35         return
36     for child in active_children:
37         LOG.debug("%s %s %s, child: %s %s", current_proccess.name, current_proccess.pid,
38                   os.getpid(), child, child.pid)
39         LOG.debug("joining %s", child)
40         child.join(timeout)
41         child.terminate()
42     active_children = multiprocessing.active_children()
43     if not active_children:
44         LOG.debug("no children to terminate")
45     for child in active_children:
46         LOG.debug("%s %s %s, after terminate child: %s %s", current_proccess.name,
47                   current_proccess.pid, os.getpid(), child, child.pid)