MCPcopy Create free account
hub / github.com/RedisGears/RedisGears / RedisGearsPy_PyCallbackMapper

Function RedisGearsPy_PyCallbackMapper

plugins/python/redisgears_python.c:5539–5601  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5537}
5538
5539static Record* RedisGearsPy_PyCallbackMapper(ExecutionCtx* rctx, Record *record, void* arg){
5540 RedisModule_Assert(RedisGears_RecordGetType(record) == pythonRecordType);
5541
5542 PythonSessionCtx* sctx = RedisGears_GetFlatExecutionPrivateData(rctx);
5543 RedisModule_Assert(sctx);
5544
5545 PythonExecutionCtx pectx = PythonExecutionCtx_New(sctx, rctx);
5546 RedisGearsPy_Lock(&pectx);
5547
5548 PyObject* pArgs = PyTuple_New(1);
5549 PyObject* callback = arg;
5550 PyObject* oldObj = PyObjRecordGet(record);
5551 PyObjRecordSet(record, NULL); // pass ownership of oldObj to NULL
5552 PyTuple_SetItem(pArgs, 0, oldObj);
5553 if (RedisGears_ProfileEnabled()) {
5554 // start profiling
5555 PyObject_CallFunction(profileStartFunction, "O", sctx->profiler);
5556 }
5557 PyObject* newObj = PyObject_CallObject(callback, pArgs);
5558 if (RedisGears_ProfileEnabled()) {
5559 // start profiling
5560 PyObject_CallFunction(profileStopFunction, "O", sctx->profiler);
5561 }
5562 GearsPyDecRef(pArgs);
5563
5564 PythonThreadCtx* ptctx = GetPythonThreadCtx();
5565
5566 if(newObj && PyCoro_CheckExact(newObj)){
5567 // object is a coroutine, we need to hold and pass it to the event loop.
5568 newObj = RedisGearsPy_PyCallbackHandleCoroutine(rctx, newObj, ptctx);
5569 }
5570
5571 if(ptctx->pyfutureCreated && newObj == ptctx->pyfutureCreated){
5572 PyFuture* future = (PyFuture*)newObj;
5573 future->continueType = ContinueType_Default;
5574 GearsPyDecRef(newObj);
5575 RedisGears_FreeRecord(record);
5576 RedisGearsPy_Unlock(&pectx);
5577 return RedisGears_GetDummyRecord();
5578 }
5579
5580 if(ptctx->pyfutureCreated){
5581 /**
5582 * Async record created but not returned lets disscard it
5583 */
5584 PyFuture* f = (PyFuture*)ptctx->pyfutureCreated;
5585 RedisGears_AsyncRecordContinue(f->asyncRecord, RedisGears_GetDummyRecord());
5586 f->asyncRecord = NULL;
5587 }
5588
5589 if(!newObj){
5590 fetchPyError(rctx);
5591
5592 RedisGearsPy_Unlock(&pectx);
5593 RedisGears_FreeRecord(record);
5594 return NULL;
5595 }
5596

Callers

nothing calls this directly

Calls 8

RedisGearsPy_LockFunction · 0.85
PyObjRecordGetFunction · 0.85
PyObjRecordSetFunction · 0.85
GearsPyDecRefFunction · 0.85
GetPythonThreadCtxFunction · 0.85
RedisGearsPy_UnlockFunction · 0.85
fetchPyErrorFunction · 0.85

Tested by

no test coverage detected