@@ -14,6 +14,7 @@ const char *PythonSo = "libpython3.so";
14
14
15
15
wrapperMeterCustom g_metric_cb;
16
16
wrapperTraceLog g_trace_cb;
17
+ wrapperCallback g_resp_cb;
17
18
18
19
std::mutex RECORD_MUTEX;
19
20
std::map <std::string, std::string> SID_RECORD;
@@ -127,10 +128,11 @@ PyWrapper::PyWrapper() {
127
128
_wrapperCreate = _obj.attr (" wrapperCreate" );
128
129
_wrapperWrite = _obj.attr (" wrapperWrite" );
129
130
_wrapperRead = _obj.attr (" wrapperRead" );
131
+ _wrapperDestroy = _obj.attr (" wrapperDestroy" );
130
132
_wrapperTest = _obj.attr (" wrapperTestFunc" );
131
133
132
134
py::gil_scoped_release release;
133
- StartMonitorWrapperClass (_wrapper_abs);
135
+ // StartMonitorWrapperClass(_wrapper_abs);
134
136
135
137
}
136
138
@@ -205,7 +207,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) {
205
207
s.push_back (wrapperFileAbs);
206
208
std::map <std::string, EventHandle> funs;
207
209
208
- // 变化后,重载wrapper
210
+ /* * 变化后,重载wrapper * */
209
211
funs.insert ({" IN_MOVE_SELF" , reloadWrapper});
210
212
ino->InitWatchFile (s, this );
211
213
int ret = ino->StartWatchThread (funs, _pid);
@@ -250,7 +252,7 @@ int PyWrapper::wrapperFini() {
250
252
251
253
int PyWrapper::wrapperOnceExec (const char *usrTag, std::map <std::string, std::string> params, DataListCls reqData,
252
254
pDataList *respData, std::string sid, wrapperCallback cb, unsigned int psrId) {
253
- SetSidUsrTag (sid, usrTag);
255
+ // SetSidUsrTag(sid, usrTag);
254
256
try {
255
257
if (cb != nullptr ) {
256
258
SetSidCallBack (cb, sid);
@@ -347,13 +349,12 @@ int PyWrapper::wrapperOnceExecAsync(const char *usrTag, std::map <std::string, s
347
349
try {
348
350
if (cb != nullptr ) {
349
351
SetSidCallBack (cb, sid);
350
-
351
352
}
353
+
352
354
int ret = 0 ;
353
- SetSidUsrTag (sid, usrTag);
354
355
params[" sid" ] = sid;
355
356
// 执行python exec 推理
356
- py::object r = _wrapperOnceExecAsync (params, reqData, sid , psrId);
357
+ py::object r = _wrapperOnceExecAsync (params, reqData, usrTag , psrId);
357
358
// 此段根据python的返回 ,回写 respData
358
359
spdlog::info (" start wrapperExecAsync cast python resp to c++ object, thread_id: {}, sid: {}" , gettid (), sid);
359
360
ret = r.cast <int >();
@@ -410,12 +411,11 @@ PyWrapper::wrapperCreate(const char *usrTag, std::map <std::string, std::string>
410
411
int *errNum, std::string sid, unsigned int psrId) {
411
412
SessionCreateResponse *resp;
412
413
SetSidCallBack (cb, sid);
413
- SetSidUsrTag (sid, usrTag);
414
414
415
415
try {
416
416
py::gil_scoped_acquire acquire;
417
417
// 此段根据python的返回 ,回写 respData
418
- py::object r = _wrapperCreate (params, sid, psrId);
418
+ py::object r = _wrapperCreate (params, sid, psrId, usrTag );
419
419
resp = r.cast <SessionCreateResponse *>();
420
420
*errNum = resp->errCode ;
421
421
if (*errNum != 0 ) {
@@ -436,11 +436,11 @@ PyWrapper::wrapperCreate(const char *usrTag, std::map <std::string, std::string>
436
436
}
437
437
438
438
// 上行数据
439
- int PyWrapper::wrapperWrite (char *handle, DataListCls reqData, std::string sid ) {
439
+ int PyWrapper::wrapperWrite (char *handle, DataListCls reqData) {
440
440
try {
441
441
int ret = 0 ;
442
442
// 执行python exec 推理
443
- py::object r = _wrapperWrite (handle, reqData, sid );
443
+ py::object r = _wrapperWrite (handle, reqData);
444
444
ret = r.cast <int >();
445
445
return ret;
446
446
}
@@ -455,12 +455,12 @@ int PyWrapper::wrapperWrite(char *handle, DataListCls reqData, std::string sid)
455
455
}
456
456
457
457
// 下行数据
458
- int PyWrapper::wrapperRead (char *handle, pDataList *respData, std::string sid ) {
458
+ int PyWrapper::wrapperRead (char *handle, pDataList *respData) {
459
459
try {
460
460
Response *resp;
461
461
// 执行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 );
464
464
resp = r.cast <Response *>();
465
465
pDataList headPtr;
466
466
pDataList curPtr;
@@ -489,7 +489,7 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
489
489
pr = malloc (itemData.len );
490
490
if (pr == nullptr ) {
491
491
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 );
493
493
return ret;
494
494
}
495
495
memcpy (pr, (const void *) itemData.data .ptr (), itemData.len );
@@ -504,9 +504,9 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
504
504
curPtr->next = tmpData;
505
505
curPtr = tmpData;
506
506
}
507
- spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid :{}" ,
507
+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},handle :{}" ,
508
508
tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
509
- tmpData->status , sid );
509
+ tmpData->status , handle );
510
510
}
511
511
*respData = headPtr;
512
512
}
@@ -521,20 +521,19 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
521
521
return 0 ;
522
522
}
523
523
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;
528
530
}
529
531
530
532
int PyWrapper::wrapperExecFree (const char *usrTag) {
531
- std::string sid = GetSidByUsrTag (usrTag);
532
- if (sid != " " )
533
- DelSidUsrTag (sid);
534
533
return 0 ;
535
534
}
536
535
537
- int PyWrapper::wrapperLoadRes (pDataList p, unsigned int resId ) {
536
+ int PyWrapper::wrapperLoadRes (pDataList p, std::string patch_id ) {
538
537
DataListCls perData;
539
538
// 构建请求数据
540
539
int dataNum = 0 ;
@@ -554,21 +553,21 @@ int PyWrapper::wrapperLoadRes(pDataList p, unsigned int resId) {
554
553
// char t = static_cast<int>(p->type);
555
554
item.type = p->type ;
556
555
item.status = p->status ;
557
- spdlog::debug (" reqDatatype :{},resID :{}" , p->type , resId );
556
+ spdlog::debug (" reqDatatype :{},patch_id :{}" , p->type , patch_id );
558
557
perData.list .push_back (item);
559
558
p = p->next ;
560
559
}
561
560
}
562
561
py::gil_scoped_acquire acquire;
563
562
// 执行python exec 推理
564
- int ret = _wrapperLoadRes (perData, resId ).cast <int >();;
563
+ int ret = _wrapperLoadRes (perData, patch_id ).cast <int >();;
565
564
return ret;
566
565
}
567
566
568
- int PyWrapper::wrapperUnloadRes (unsigned int resId ) {
567
+ int PyWrapper::wrapperUnloadRes (std::string patch_id ) {
569
568
py::gil_scoped_acquire acquire;
570
569
// 执行python exec 推理
571
- int ret = _wrapperUnloadRes (resId ).cast <int >();;
570
+ int ret = _wrapperUnloadRes (patch_id ).cast <int >();;
572
571
return ret;
573
572
}
574
573
@@ -595,7 +594,6 @@ int PyWrapper::wrapperTest() {
595
594
}
596
595
for (size_t i = 0 ; i < l->list .size (); ++i) {
597
596
ResponseData d = l->list [i];
598
- // std::cout << "Response key: " << d.key << std::endl;
599
597
// std::cout << "Response len" << d.len << std::endl;
600
598
// std::cout << "response actual data Size " << d.data.length() << std::endl;
601
599
@@ -620,21 +618,21 @@ int callbackTrace(const char *usrTag, const char *key, const char *value) {
620
618
return g_trace_cb (usrTag, key, value);
621
619
}
622
620
623
- int callBack (Response *resp, std::string sid ) {
621
+ int callBack (Response *resp, char *usrTag ) {
624
622
wrapperCallback cb_;
625
- cb_ = GetSidCB (sid) ;
623
+ cb_ = g_resp_cb ;
626
624
if (cb_ == NULL ) {
627
625
printf (" null cb....\n " );
628
626
return -1 ;
629
627
}
630
- const char *usrTag = GetSidUsrTag (sid);
631
628
632
629
pDataList headPtr = nullptr ;
633
630
pDataList curPtr = nullptr ;
634
631
// 先判断python有没有抛出错误. response中的 errorCode
635
632
if (resp->errCode != 0 ) {
636
633
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;
638
636
}
639
637
char *ptr;
640
638
int dataSize = resp->list .size ();
@@ -656,7 +654,7 @@ int callBack(Response *resp, std::string sid) {
656
654
pr = malloc (itemData.len );
657
655
if (pr == nullptr ) {
658
656
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 );
660
658
return ret;
661
659
}
662
660
ptr = PyBytes_AsString (itemData.data .ptr ());
@@ -674,9 +672,9 @@ int callBack(Response *resp, std::string sid) {
674
672
curPtr->next = tmpData;
675
673
curPtr = tmpData;
676
674
}
677
- spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},sid :{}" ,
675
+ spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},usrTag :{}" ,
678
676
tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
679
- tmpData->status , sid );
677
+ tmpData->status , usrTag );
680
678
}
681
679
682
680
int cb_ret = cb_ (usrTag, headPtr, 0 );
@@ -698,17 +696,12 @@ int callBack(Response *resp, std::string sid) {
698
696
}
699
697
700
698
void SetSidCallBack (wrapperCallback cb, std::string sid) {
701
- RECORD_MUTEX.lock ();
702
- SID_CB[sid] = cb;
703
- RECORD_MUTEX.unlock ();
699
+ g_resp_cb = cb;
704
700
}
705
701
706
702
wrapperCallback 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;
712
705
}
713
706
714
707
void SetSidUsrTag (std::string sid, const char *usrTag) {
0 commit comments