Example #1
0
 def _yield_child_results(self, child):
     """Yield results from a child
     
     for use in iterator methods
     """
     rlist = child.result()
     if not isinstance(rlist, list):
         rlist = [rlist]
     error.collect_exceptions(rlist, self._fname)
     for r in rlist:
         yield r
Example #2
0
 def _yield_child_results(self, child):
     """Yield results from a child
     
     for use in iterator methods
     """
     rlist = child.result()
     if not isinstance(rlist, list):
         rlist = [rlist]
     error.collect_exceptions(rlist, self._fname)
     for r in rlist:
         yield r
Example #3
0
 def _yield_child_results(self, child):
     """Yield results from a child
     
     for use in iterator methods
     """
     rlist = child.result()
     error.collect_exceptions(rlist, self._fname)
     try:
         for r in rlist:
             yield r
     except TypeError:
         # flattened, not a list
         # this could get broken by flattened data that returns iterables
         # but most calls to map do not expose the `flatten` argument
         yield rlist
Example #4
0
    def wait(self, timeout=-1):
        """Wait until the result is available or until `timeout` seconds pass.

        This method always returns None.
        """
        if self._ready:
            return
        self._ready = self._client._await_futures(self._futures, timeout)
        if self._ready:
            try:
                results = list(map(self._client.results.get, self.msg_ids))
                self._result = results
                if self._single_result:
                    r = results[0]
                    if isinstance(r, Exception):
                        raise r
                else:
                    results = error.collect_exceptions(results, self._fname)
                self._result = self._reconstruct_result(results)
            except Exception as e:
                self._exception = e
                self._success = False
            else:
                self._success = True
            finally:
                self._metadata = [self._client.metadata[mid] for mid in self.msg_ids]
                self.wait_for_output(0)
                if self.owner:
                    [ self._client.results.pop(mid) for mid in self.msg_ids ]
Example #5
0
    def wait(self, timeout=-1):
        """Wait until the result is available or until `timeout` seconds pass.

        This method always returns None.
        """
        if self._ready:
            return
        self._ready = self._client._await_futures(self._futures, timeout)
        if self._ready:
            try:
                results = list(map(self._client.results.get, self.msg_ids))
                self._result = results
                if self._single_result:
                    r = results[0]
                    if isinstance(r, Exception):
                        raise r
                else:
                    results = error.collect_exceptions(results, self._fname)
                self._result = self._reconstruct_result(results)
            except Exception as e:
                self._exception = e
                self._success = False
            else:
                self._success = True
            finally:
                self._metadata = [
                    self._client.metadata[mid] for mid in self.msg_ids
                ]
                self.wait_for_output(0)
                if self.owner:
                    [self._client.results.pop(mid) for mid in self.msg_ids]
Example #6
0
 def __iter__(self):
     if self._single_result:
         raise TypeError("AsyncResults with a single result are not iterable.")
     try:
         rlist = self.get(0)
     except error.TimeoutError:
         # wait for each result individually
         evt = Event()
         for child in self._children:
             self._wait_for_child(child, evt=evt)
             result = child.result()
             error.collect_exceptions([result], self._fname)
             yield result
     else:
         # already done
         for r in rlist:
             yield r
Example #7
0
 def __iter__(self):
     if self._single_result:
         raise TypeError("AsyncResults with a single result are not iterable.")
     try:
         rlist = self.get(0)
     except error.TimeoutError:
         # wait for each result individually
         evt = Event()
         for child in self._children:
             self._wait_for_child(child, evt=evt)
             result = child.result()
             error.collect_exceptions([result], self._fname)
             yield result
     else:
         # already done
         for r in rlist:
             yield r
Example #8
0
    def _collect_exceptions(self, results):
        """Wrap Exceptions in a CompositeError

        if self._return_exceptions is True, this is a no-op
        """
        if self._return_exceptions:
            return results
        else:
            return error.collect_exceptions(results, self._fname)
Example #9
0
 def __getitem__(self, key):
     """getitem returns result value(s) if keyed by int/slice, or metadata if key is str.
     """
     if isinstance(key, int):
         self._check_ready()
         return error.collect_exceptions([self._result[key]], self._fname)[0]
     elif isinstance(key, slice):
         self._check_ready()
         return error.collect_exceptions(self._result[key], self._fname)
     elif isinstance(key, string_types):
         # metadata proxy *does not* require that results are done
         self.wait(0)
         values = [ md[key] for md in self._metadata ]
         if self._single_result:
             return values[0]
         else:
             return values
     else:
         raise TypeError("Invalid key type %r, must be 'int','slice', or 'str'"%type(key))
Example #10
0
 def __getitem__(self, key):
     """getitem returns result value(s) if keyed by int/slice, or metadata if key is str.
     """
     if isinstance(key, int):
         self._check_ready()
         return error.collect_exceptions([self.result()[key]], self._fname)[0]
     elif isinstance(key, slice):
         self._check_ready()
         return error.collect_exceptions(self.result()[key], self._fname)
     elif isinstance(key, string_types):
         # metadata proxy *does not* require that results are done
         self.wait(0)
         self.wait_for_output(0)
         values = [ md[key] for md in self._metadata ]
         if self._single_result:
             return values[0]
         else:
             return values
     else:
         raise TypeError("Invalid key type %r, must be 'int','slice', or 'str'"%type(key))
Example #11
0
 def wait(self, timeout=-1):
     """wait for result to complete."""
     start = time.time()
     if self._ready:
         return
     local_ids = [m for m in self.msg_ids if m in self._client.outstanding]
     local_ready = self._client.wait(local_ids, timeout)
     if local_ready:
         remote_ids = [
             m for m in self.msg_ids if m not in self._client.results
         ]
         if not remote_ids:
             self._ready = True
         else:
             rdict = self._client.result_status(remote_ids,
                                                status_only=False)
             pending = rdict['pending']
             while pending and (timeout < 0
                                or time.time() < start + timeout):
                 rdict = self._client.result_status(remote_ids,
                                                    status_only=False)
                 pending = rdict['pending']
                 if pending:
                     time.sleep(0.1)
             if not pending:
                 self._ready = True
     if self._ready:
         self._output_ready = True
         try:
             results = list(map(self._client.results.get, self.msg_ids))
             self._result = results
             if self._single_result:
                 r = results[0]
                 if isinstance(r, Exception):
                     raise r
             else:
                 results = error.collect_exceptions(results, self._fname)
             self._result = self._reconstruct_result(results)
         except Exception as e:
             self._exception = e
             self._success = False
         else:
             self._success = True
         finally:
             self._metadata = [
                 self._client.metadata[mid] for mid in self.msg_ids
             ]
             if self.owner:
                 [self._client.metadata.pop(mid) for mid in self.msg_ids]
                 [self._client.results.pop(mid) for mid in self.msg_ids]
Example #12
0
 def _resolve_result(self, f=None):
     try:
         if f:
             results = f.result()
         else:
             results = list(map(self._client.results.get, self.msg_ids))
         if self._single_result:
             r = results[0]
             if isinstance(r, Exception):
                 raise r
         else:
             results = error.collect_exceptions(results, self._fname)
         self._success = True
         self.set_result(self._reconstruct_result(results))
     except Exception as e:
         self._success = False
         self.set_exception(e)
Example #13
0
 def _resolve_result(self, f=None):
     try:
         if f:
             results = f.result()
         else:
             results = list(map(self._client.results.get, self.msg_ids))
         if self._single_result:
             r = results[0]
             if isinstance(r, Exception):
                 raise r
         else:
             results = error.collect_exceptions(results, self._fname)
         self._success = True
         self.set_result(self._reconstruct_result(results))
     except Exception as e:
         self._success = False
         self.set_exception(e)
Example #14
0
 def wait(self, timeout=-1):
     """wait for result to complete."""
     start = time.time()
     if self._ready:
         return
     local_ids = [m for m in self.msg_ids if m in self._client.outstanding]
     local_ready = self._client.wait(local_ids, timeout)
     if local_ready:
         remote_ids = [m for m in self.msg_ids if m not in self._client.results]
         if not remote_ids:
             self._ready = True
         else:
             rdict = self._client.result_status(remote_ids, status_only=False)
             pending = rdict['pending']
             while pending and (timeout < 0 or time.time() < start+timeout):
                 rdict = self._client.result_status(remote_ids, status_only=False)
                 pending = rdict['pending']
                 if pending:
                     time.sleep(0.1)
             if not pending:
                 self._ready = True
     if self._ready:
         self._output_ready = True
         try:
             results = list(map(self._client.results.get, self.msg_ids))
             self._result = results
             if self._single_result:
                 r = results[0]
                 if isinstance(r, Exception):
                     raise r
             else:
                 results = error.collect_exceptions(results, self._fname)
             self._result = self._reconstruct_result(results)
         except Exception as e:
             self._exception = e
             self._success = False
         else:
             self._success = True
         finally:
             self._metadata = [self._client.metadata[mid] for mid in self.msg_ids]
             if self.owner:
                 [self._client.metadata.pop(mid) for mid in self.msg_ids]
                 [self._client.results.pop(mid) for mid in self.msg_ids]