[Core] Add private on_completed callback for ObjectRef (#13688)

This commit is contained in:
Simon Mo
2021-01-27 16:32:00 -08:00
committed by GitHub
parent 32ec0d205f
commit 25fa391193
3 changed files with 70 additions and 36 deletions
+12 -30
View File
@@ -1569,12 +1569,13 @@ cdef class CoreWorker:
return ref_counts
def get_async(self, ObjectRef object_ref, future):
cpython.Py_INCREF(future)
def set_get_async_callback(self, ObjectRef object_ref, callback):
cpython.Py_INCREF(callback)
CCoreWorkerProcess.GetCoreWorker().GetAsync(
object_ref.native(),
async_set_result,
<void*>future)
object_ref.native(),
async_callback,
<void*>callback
)
def push_error(self, JobID job_id, error_type, error_message,
double timestamp):
@@ -1588,13 +1589,11 @@ cdef class CoreWorker:
resource_name.encode("ascii"), capacity,
CNodeID.FromBinary(client_id.binary()))
cdef void async_set_result(shared_ptr[CRayObject] obj,
CObjectID object_ref,
void *future) with gil:
cdef void async_callback(shared_ptr[CRayObject] obj,
CObjectID object_ref,
void *user_callback) with gil:
cdef:
c_vector[shared_ptr[CRayObject]] objects_to_deserialize
py_future = <object>(future)
loop = py_future._loop
# Object is retrieved from in memory store.
# Here we go through the code path used to deserialize objects.
@@ -1605,23 +1604,6 @@ cdef void async_set_result(shared_ptr[CRayObject] obj,
result = ray.worker.global_worker.deserialize_objects(
data_metadata_pairs, ids_to_deserialize)[0]
def set_future():
# Issue #11030, #8841
# If this future has result set already, we just need to
# skip the set result/exception procedure.
if py_future.done():
cpython.Py_DECREF(py_future)
return
if isinstance(result, RayTaskError):
ray.worker.last_task_error_raise_time = time.time()
py_future.set_exception(result.as_instanceof_cause())
elif isinstance(result, RayError):
# Directly raise exception for RayActorError
py_future.set_exception(result)
else:
py_future.set_result(result)
cpython.Py_DECREF(py_future)
loop.call_soon_threadsafe(set_future)
py_callback = <object>user_callback
py_callback(result)
cpython.Py_DECREF(py_callback)