Skip to content

Commit 44c3cb2

Browse files
authored
Merge pull request #5 from iflytek/grpc
Grpc to master
2 parents 977b87e + cdd866c commit 44c3cb2

File tree

3 files changed

+104
-72
lines changed

3 files changed

+104
-72
lines changed

pyWrapper.cpp

+37-44
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const char *PythonSo = "libpython3.so";
1414

1515
wrapperMeterCustom g_metric_cb;
1616
wrapperTraceLog g_trace_cb;
17+
wrapperCallback g_resp_cb;
1718

1819
std::mutex RECORD_MUTEX;
1920
std::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

251253
int 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

530532
int 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

700698
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;
704700
}
705701

706702
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;
712705
}
713706

714707
void SetSidUsrTag(std::string sid, const char *usrTag) {

pyWrapper.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,19 @@ class PyWrapper {
138138

139139
int wrapperSetTraceFunc(CtrlType type, wrapperTraceLog mc);
140140

141-
int wrapperWrite(char *handle, DataListCls reqData, std::string sid);
141+
int wrapperWrite(char *handle, DataListCls reqData);
142142

143-
int wrapperRead(char *handle, pDataList *respData, std::string sid);
143+
int wrapperRead(char *handle, pDataList *respData);
144144

145-
int wrapperDestroy(std::string sid);
145+
int wrapperDestroy( char * handle);
146146

147147
int wrapperExecFree(const char *usrTag);
148148

149149
int wrapperTest();
150150

151-
int wrapperLoadRes(pDataList perData, unsigned int resId);
151+
int wrapperLoadRes(pDataList perData, std::string resId);
152152

153-
int wrapperUnloadRes(unsigned int resId);
153+
int wrapperUnloadRes(std::string resId);
154154

155155
void setCallBack(wrapperCallback cb);
156156

@@ -166,6 +166,8 @@ class PyWrapper {
166166
py::object _wrapperOnceExecAsync;
167167
py::object _wrapperError;
168168
py::object _wrapperCreate;
169+
py::object _wrapperDestroy;
170+
169171
py::object _wrapperWrite;
170172
py::object _wrapperRead;
171173
py::object _wrapperLoadRes;
@@ -184,7 +186,7 @@ std::string GetHandleSid(char *handle);
184186
void DelHandleSid(char *handle);
185187

186188

187-
int callBack(Response *respData, std::string);
189+
int callBack(Response *respData, char *usrTag);
188190

189191
int callbackMetric(const char *usrTag, const char *meterKey, int count);
190192

0 commit comments

Comments
 (0)