在Python中使用Golang方法时内存不足

问题描述 投票:1回答:1

我一直关注这个https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf

现在,我在做的是: -

  • 传递Json字符串
  • 将Json Unmarshall与Go Struct Reference和字符串一起使用
  • 做业务逻辑和运营
  • 将结构编组为JSON String
  • 返回

现在,我面临的问题是,当我通过python运行它时,我得到一个Out-of-Memory异常。如果我通过正常的Go运行它,那么它运行良好。

此外,如果我不解组并只是正常初始化结构然后我没有问题,并获得一个json字符串。但我需要在两者之间传递结构化数据。

问题发生在Unmarshal Line

func BuildCRMLinkFromJSON(jsonstring string) CRMLinkBO {
    var crmLink CRMLink = CRMLink{}
    json.Unmarshal([]byte(jsonstring), &crmLink)
    var clb = CRMLinkBO{crmLink: crmLink}
    return clb
}

运行时错误是:

runtime: out of memory: cannot allocate 140328041390080-byte block (66781184 in use)
fatal error: out of memory

要么

Shauryas-MacBook-Pro:go-python-interfacing xuryax$ python pythonclient.py 
unexpected fault address 0x210c1a334
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0x210c1a334 pc=0x1087b0c75]

最小再现:usego.go

package main

import (
    "encoding/json"
    "fmt"
)

import "C"

// Data Model

type CustomField struct {
    Field string `json:"field"`
    Value string `json:"value"`
}

type PersonDetails struct {
    Person_id string        `json:"person_id"`
    Detail    []CustomField `json:"detail"`
}

func New(pid string) PersonDetails {
    var p = PersonDetails{"HARDCODED PID IN GO", []CustomField{}}
    return p
}

func BuildFromJSON(jsonString string) PersonDetails {
    var person_detail PersonDetails = PersonDetails{}
    json.Unmarshal([]byte(jsonString), &person_detail)
    return person_detail
}

func ConvertToJSON(p PersonDetails) string {
    fmt.Println(p)
    var je, _ = json.Marshal(p)
    return string(je)
}

func AddValue(p PersonDetails) PersonDetails {
    var CustField = CustomField{"Hardcoded Field in Go", "Hardcoded Value in Gos"}
    p.Detail = append(p.Detail, CustField)
    return p
}

//export DoJSONOperation
func DoJSONOperation(jsonString string) *C.char {
    var p = BuildFromJSON(jsonString)
    p = AddValue(p)
    var nstr = ConvertToJSON(p)
    return C.CString(nstr)
}

//export DoNormalOperation
func DoNormalOperation(jsonString string) *C.char {
    var p = New(jsonString)
    p = AddValue(p)
    var nstr = ConvertToJSON(p)
    fmt.Println("Before Return: ", string(nstr))
    return C.CString(nstr)
}

func main() {
    // DO noting
    // var jsonStr = `{"person_id":"JsonID","detail":[{"field":"json passed field", "value":"json passed value"}]}`
}

Python client.朋友

from ctypes import *
import json

def main():
    lib = cdll.LoadLibrary("/Users/xuryax/work/repos/research/go-python-interfacing/usego.so")

    lib.DoJSONOperation.argtypes = [c_char_p]
    lib.DoJSONOperation.restype = c_char_p

    json_string = """{"person_id":"PythonJsonID","detail":[{"field":"json passed field", "value":"json passed value"}]}"""
    normal_pid = "Python ID"
    updated_json = lib.DoNormalOperation(normal_pid)

    print(updated_json)


if __name__=="__main__":
    main()

我发现如果我使用硬编码的那个奇怪的东西就是返回指针(整数)但是在我之前的实验中,我确实得到了一个字符串。

python-2.7 go
1个回答
0
投票

答案看起来很简单。这是我错过的类型转换问题。

该方法应接受*C.char

//export DoJSONOperation
func DoJSONOperation(jsonString *C.char) *C.char {
    var GoString = C.GoString(jsonString)
    var p = BuildFromJSON(GoString)
    p = AddValue(p)
    var nstr = ConvertToJSON(p)
    return C.CString(nstr)
}

这似乎解决了这个问题。

在Python中我们也需要进行类型转换。而不是发送Python字符串,我们需要发送。 c_char_p(jsonString)

热门问题
推荐问题
最新问题