X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FPLRsearch%2FPLRsearch.py;h=ec58fbd10f61c9136c01539e229960daec9a729e;hb=f58d415afaacc7565f08817903b0d21f16579eb8;hp=e20d293d3c0f8425c9a8c826c914b12e8726167e;hpb=d68951ac245150eeefa6e0f4156e4c1b5c9e9325;p=csit.git diff --git a/resources/libraries/python/PLRsearch/PLRsearch.py b/resources/libraries/python/PLRsearch/PLRsearch.py index e20d293d3c..ec58fbd10f 100644 --- a/resources/libraries/python/PLRsearch/PLRsearch.py +++ b/resources/libraries/python/PLRsearch/PLRsearch.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 Cisco and/or its affiliates. +# Copyright (c) 2020 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -57,6 +57,8 @@ class PLRsearch: trial_number_offset=0, timeout=1800.0, trace_enabled=False): """Store rate measurer and additional parameters. + The measurer must never report negative loss count. + TODO: Copy AbstractMeasurer from MLRsearch. :param measurer: The measurer to call when searching. @@ -599,15 +601,17 @@ class PLRsearch: dilled_function = dill.dumps(value_logweight_func) boss_pipe_end, worker_pipe_end = multiprocessing.Pipe() - boss_pipe_end.send( - (dimension, dilled_function, focus_tracker, max_samples) - ) + # Do not send yet, run the worker first to avoid a deadlock. + # See https://stackoverflow.com/a/15716500 worker = multiprocessing.Process( target=Integrator.try_estimate_nd, args=(worker_pipe_end, 10.0, self.trace_enabled) ) worker.daemon = True worker.start() + boss_pipe_end.send( + (dimension, dilled_function, focus_tracker, max_samples) + ) return boss_pipe_end erf_pipe = start_computing(self.lfit_erf, erf_focus_tracker) @@ -633,7 +637,15 @@ class PLRsearch: and number of samples used for this iteration. :rtype: _PartialResult """ - pipe.send(None) + # If worker encountered an exception, we get it in the recv below, + # but send will report a broken pipe. + # EAFP says we should ignore the error (instead of polling first). + # https://devblogs.microsoft.com/python + # /idiomatic-python-eafp-versus-lbyl/ + try: + pipe.send(None) + except BrokenPipeError: + pass if not pipe.poll(10.0): raise RuntimeError(f"Worker {name} did not finish!") result_or_traceback = pipe.recv()