@@ -14,6 +14,7 @@ const char *PythonSo = "libpython3.so";
1414
1515wrapperMeterCustom g_metric_cb;
1616wrapperTraceLog g_trace_cb;
17+ wrapperCallback g_resp_cb;
1718
1819std::mutex RECORD_MUTEX;
1920std::map <std::string, std::string> SID_RECORD;
@@ -127,10 +128,11 @@ PyWrapper::PyWrapper() {
127128 _wrapperCreate = _obj.attr (" wrapperCreate" );
128129 _wrapperWrite = _obj.attr (" wrapperWrite" );
129130 _wrapperRead = _obj.attr (" wrapperRead" );
131+ _wrapperDestroy = _obj.attr (" wrapperDestroy" );
130132 _wrapperTest = _obj.attr (" wrapperTestFunc" );
131133
132134 py::gil_scoped_release release;
133- StartMonitorWrapperClass (_wrapper_abs);
135+ // StartMonitorWrapperClass(_wrapper_abs);
134136
135137}
136138
@@ -205,7 +207,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) {
205207 s.push_back (wrapperFileAbs);
206208 std::map <std::string, EventHandle> funs;
207209
208- // 变化后,重载wrapper
210+ /* * 变化后,重载wrapper * */
209211 funs.insert ({" IN_MOVE_SELF" , reloadWrapper});
210212 ino->InitWatchFile (s, this );
211213 int ret = ino->StartWatchThread (funs, _pid);
@@ -250,7 +252,7 @@ int PyWrapper::wrapperFini() {
250252
251253int PyWrapper::wrapperOnceExec (const char *usrTag, std::map <std::string, std::string> params, DataListCls reqData,
252254 pDataList *respData, std::string sid, wrapperCallback cb, unsigned int psrId) {
253- SetSidUsrTag (sid, usrTag);
255+ // SetSidUsrTag(sid, usrTag);
254256 try {
255257 if (cb != nullptr ) {
256258 SetSidCallBack (cb, sid);
@@ -347,13 +349,12 @@ int PyWrapper::wrapperOnceExecAsync(const char *usrTag, std::map <std::string, s
347349 try {
348350 if (cb != nullptr ) {
349351 SetSidCallBack (cb, sid);
350-
351352 }
353+
352354 int ret = 0 ;
353- SetSidUsrTag (sid, usrTag);
354355 params[" sid" ] = sid;
355356 // 执行python exec 推理
356- py::object r = _wrapperOnceExecAsync (params, reqData, sid , psrId);
357+ py::object r = _wrapperOnceExecAsync (params, reqData, usrTag , psrId);
357358 // 此段根据python的返回 ,回写 respData
358359 spdlog::info (" start wrapperExecAsync cast python resp to c++ object, thread_id: {}, sid: {}" , gettid (), sid);
359360 ret = r.cast <int >();
@@ -410,12 +411,11 @@ PyWrapper::wrapperCreate(const char *usrTag, std::map <std::string, std::string>
410411 int *errNum, std::string sid, unsigned int psrId) {
411412 SessionCreateResponse *resp;
412413 SetSidCallBack (cb, sid);
413- SetSidUsrTag (sid, usrTag);
414414
415415 try {
416416 py::gil_scoped_acquire acquire;
417417 // 此段根据python的返回 ,回写 respData
418- py::object r = _wrapperCreate (params, sid, psrId);
418+ py::object r = _wrapperCreate (params, sid, psrId, usrTag );
419419 resp = r.cast <SessionCreateResponse *>();
420420 *errNum = resp->errCode ;
421421 if (*errNum != 0 ) {
@@ -436,11 +436,11 @@ PyWrapper::wrapperCreate(const char *usrTag, std::map <std::string, std::string>
436436}
437437
438438// 上行数据
439- int PyWrapper::wrapperWrite (char *handle, DataListCls reqData, std::string sid ) {
439+ int PyWrapper::wrapperWrite (char *handle, DataListCls reqData) {
440440 try {
441441 int ret = 0 ;
442442 // 执行python exec 推理
443- py::object r = _wrapperWrite (handle, reqData, sid );
443+ py::object r = _wrapperWrite (handle, reqData);
444444 ret = r.cast <int >();
445445 return ret;
446446 }
@@ -455,12 +455,12 @@ int PyWrapper::wrapperWrite(char *handle, DataListCls reqData, std::string sid)
455455}
456456
457457// 下行数据
458- int PyWrapper::wrapperRead (char *handle, pDataList *respData, std::string sid ) {
458+ int PyWrapper::wrapperRead (char *handle, pDataList *respData) {
459459 try {
460460 Response *resp;
461461 // 执行python exec 推理
462- py::object r = _wrapperRead (handle, sid );
463- spdlog::debug (" start cast python resp to c++ object, thread_id: {}, sid : {}" , gettid (), sid );
462+ py::object r = _wrapperRead (handle);
463+ spdlog::debug (" start cast python resp to c++ object, thread_id: {}, handle : {}" , gettid (), handle );
464464 resp = r.cast <Response *>();
465465 pDataList headPtr;
466466 pDataList curPtr;
@@ -489,7 +489,7 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
489489 pr = malloc (itemData.len );
490490 if (pr == nullptr ) {
491491 int ret = -1 ;
492- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid :{}" , sid );
492+ spdlog::get (" stderr_console" )->error (" can't malloc memory for data, handle :{}" , handle );
493493 return ret;
494494 }
495495 memcpy (pr, (const void *) itemData.data .ptr (), itemData.len );
@@ -504,9 +504,9 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
504504 curPtr->next = tmpData;
505505 curPtr = tmpData;
506506 }
507- spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid :{}" ,
507+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},handle :{}" ,
508508 tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
509- tmpData->status , sid );
509+ tmpData->status , handle );
510510 }
511511 *respData = headPtr;
512512 }
@@ -521,20 +521,19 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
521521 return 0 ;
522522}
523523
524- int PyWrapper::wrapperDestroy (std::string sid) {
525- DelSidCallback (sid);
526- DelSidUsrTag (sid);
527- return 0 ;
524+ int PyWrapper::wrapperDestroy (char * handle) {
525+ py::object r = _wrapperDestroy (handle);
526+ // 此段根据python的返回 ,回写 respData
527+ spdlog::info (" Destroy .. thread_id: {}, handle: {}" , gettid (), handle);
528+ int ret = r.cast <int >();
529+ return ret;
528530}
529531
530532int PyWrapper::wrapperExecFree (const char *usrTag) {
531- std::string sid = GetSidByUsrTag (usrTag);
532- if (sid != " " )
533- DelSidUsrTag (sid);
534533 return 0 ;
535534}
536535
537- int PyWrapper::wrapperLoadRes (pDataList p, unsigned int resId ) {
536+ int PyWrapper::wrapperLoadRes (pDataList p, std::string patch_id ) {
538537 DataListCls perData;
539538 // 构建请求数据
540539 int dataNum = 0 ;
@@ -554,21 +553,21 @@ int PyWrapper::wrapperLoadRes(pDataList p, unsigned int resId) {
554553 // char t = static_cast<int>(p->type);
555554 item.type = p->type ;
556555 item.status = p->status ;
557- spdlog::debug (" reqDatatype :{},resID :{}" , p->type , resId );
556+ spdlog::debug (" reqDatatype :{},patch_id :{}" , p->type , patch_id );
558557 perData.list .push_back (item);
559558 p = p->next ;
560559 }
561560 }
562561 py::gil_scoped_acquire acquire;
563562 // 执行python exec 推理
564- int ret = _wrapperLoadRes (perData, resId ).cast <int >();;
563+ int ret = _wrapperLoadRes (perData, patch_id ).cast <int >();;
565564 return ret;
566565}
567566
568- int PyWrapper::wrapperUnloadRes (unsigned int resId ) {
567+ int PyWrapper::wrapperUnloadRes (std::string patch_id ) {
569568 py::gil_scoped_acquire acquire;
570569 // 执行python exec 推理
571- int ret = _wrapperUnloadRes (resId ).cast <int >();;
570+ int ret = _wrapperUnloadRes (patch_id ).cast <int >();;
572571 return ret;
573572}
574573
@@ -595,7 +594,6 @@ int PyWrapper::wrapperTest() {
595594 }
596595 for (size_t i = 0 ; i < l->list .size (); ++i) {
597596 ResponseData d = l->list [i];
598- // std::cout << "Response key: " << d.key << std::endl;
599597 // std::cout << "Response len" << d.len << std::endl;
600598 // std::cout << "response actual data Size " << d.data.length() << std::endl;
601599
@@ -620,21 +618,21 @@ int callbackTrace(const char *usrTag, const char *key, const char *value) {
620618 return g_trace_cb (usrTag, key, value);
621619}
622620
623- int callBack (Response *resp, std::string sid ) {
621+ int callBack (Response *resp, char *usrTag ) {
624622 wrapperCallback cb_;
625- cb_ = GetSidCB (sid) ;
623+ cb_ = g_resp_cb ;
626624 if (cb_ == NULL ) {
627625 printf (" null cb....\n " );
628626 return -1 ;
629627 }
630- const char *usrTag = GetSidUsrTag (sid);
631628
632629 pDataList headPtr = nullptr ;
633630 pDataList curPtr = nullptr ;
634631 // 先判断python有没有抛出错误. response中的 errorCode
635632 if (resp->errCode != 0 ) {
636633 spdlog::get (" stderr_console" )->error (" find error from python: {}" , resp->errCode );
637- return resp->errCode ;
634+ ret = cb_ (usrTag, NULL ,resp->errCode );
635+ return ret;
638636 }
639637 char *ptr;
640638 int dataSize = resp->list .size ();
@@ -656,7 +654,7 @@ int callBack(Response *resp, std::string sid) {
656654 pr = malloc (itemData.len );
657655 if (pr == nullptr ) {
658656 int ret = -1 ;
659- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid :{}" , sid );
657+ spdlog::get (" stderr_console" )->error (" can't malloc memory for data, usrTag :{}" , usrTag );
660658 return ret;
661659 }
662660 ptr = PyBytes_AsString (itemData.data .ptr ());
@@ -674,9 +672,9 @@ int callBack(Response *resp, std::string sid) {
674672 curPtr->next = tmpData;
675673 curPtr = tmpData;
676674 }
677- spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},sid :{}" ,
675+ spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},usrTag :{}" ,
678676 tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
679- tmpData->status , sid );
677+ tmpData->status , usrTag );
680678 }
681679
682680 int cb_ret = cb_ (usrTag, headPtr, 0 );
@@ -698,17 +696,12 @@ int callBack(Response *resp, std::string sid) {
698696}
699697
700698void SetSidCallBack (wrapperCallback cb, std::string sid) {
701- RECORD_MUTEX.lock ();
702- SID_CB[sid] = cb;
703- RECORD_MUTEX.unlock ();
699+ g_resp_cb = cb;
704700}
705701
706702wrapperCallback GetSidCB (std::string sid) {
707- wrapperCallback cb;
708- RECORD_MUTEX.lock ();
709- cb = SID_CB[sid];
710- RECORD_MUTEX.unlock ();
711- return cb;
703+ // ugly
704+ return g_resp_cb;
712705}
713706
714707void SetSidUsrTag (std::string sid, const char *usrTag) {
0 commit comments