Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
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
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
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))
if self._single_result:
r = results[0]
if isinstance(r, Exception):
raise r
self.set_result(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)
finally:
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]
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)
res,buffers = serialize.deserialize_object(buffers)
elif header['msg_type'] == 'execute_reply':
res = ExecuteReply(msg_id, rcontent, md)
else:
raise KeyError("unhandled msg type: %r" % header['msg_type'])
else:
res = self._unwrap_exception(rcontent)
failures.append(res)
self.results[msg_id] = res
content[msg_id] = res
if len(theids) == 1 and failures:
raise failures[0]
error.collect_exceptions(failures, "result_status")
return content