|
@@ -1,6 +1,7 @@
|
|
|
package services
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"hongze/hongze_edb_lib/utils"
|
|
@@ -58,7 +59,7 @@ type EdbDataFromPython struct {
|
|
|
}
|
|
|
|
|
|
// ExecPythonCode 执行Python代码
|
|
|
-func ExecPythonCode(edbCode, reqCode string) (dataMap EdbDataFromPython, err error) {
|
|
|
+func ExecPythonCode(edbCode, reqCode string) (dataMap EdbDataFromPython, err error, errMsg string) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
fmt.Println("err:", err)
|
|
@@ -85,8 +86,20 @@ func ExecPythonCode(edbCode, reqCode string) (dataMap EdbDataFromPython, err err
|
|
|
fileHandle.Close()
|
|
|
|
|
|
cmd := exec.Command(utils.PYTHON_PATH, pythonFile)
|
|
|
- outputByte, err := cmd.Output()
|
|
|
- //fmt.Println(err)
|
|
|
+ var out, errMsgOut bytes.Buffer
|
|
|
+ cmd.Stdout = &out
|
|
|
+ cmd.Stderr = &errMsgOut
|
|
|
+ err = cmd.Start()
|
|
|
+ if err != nil {
|
|
|
+ errMsg = errMsgOut.String()
|
|
|
+ }
|
|
|
+ err = cmd.Wait()
|
|
|
+ if err != nil {
|
|
|
+ errMsg = errMsgOut.String()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ outputByte := out.String()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -134,5 +147,5 @@ func getPythonFrontStr() string {
|
|
|
|
|
|
// getPythonFrontStr 获取python结尾的代码
|
|
|
func getPythonLaterStr() string {
|
|
|
- return "\nprint(\"result=\",result.to_json())\ndb.close()"
|
|
|
+ return "\nprint(\"result=\", result.to_json())\ndb.close()"
|
|
|
}
|