donglu9872 2018-08-14 15:37
浏览 119
已采纳

如何使用ctypes将数组从Go [lang]返回到Python?

I am trying to write some code that creates an array in GoLang, and returns it to a python script ctypes (and some numpy). What I have got so far doesn't work, and I cannot figure out why... I would appreciate any help!

My Go code goes something like this:

func Function(physics_stuff... float64,  N int ) []float64{
    result := make([]float64, N)
    for i:= 0; i< N; i++{
        result[i] =  blah....
    }
    return result;
}

and I am currently trying to import this functionality to python using:

from ctypes import c_double, cdll, c_int
from numpy.ctypeslib import ndpointer

lib = cdll.LoadLibrary("./go/library.so")
lib.Function.argtypes = [c_double]*6 + [c_int]

def lovely_python_function(stuff..., N):
    lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
    return lib.Function(stuff..., N)

This python function never returns. Other functions from the same library work just fine, but they all return a single float64 (c_double in python).

  • 写回答

1条回答 默认 最新

  • dsieyx2015 2018-08-18 20:46
    关注

    In your code restype is expecting _ndtpr type, see:

    lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
    

    See too in numpy document:

    def ndpointer(dtype=None, ndim=None, shape=None, flags=None)

    [others texts]

    Returns

    klass : ndpointer type object

    A type object, which is an _ndtpr instance containing
    dtype, ndim, shape and flags information.

    [others texts]

    In this way lib.Function.restype is pointer type which appropriated type in Golang must be unsafe.Pointer.

    However you want of an slice that need be passed as pointer:

    func Function(s0, s1, s2 float64, N int) unsafe.Pointer {
        result := make([]float64, N)
        for i := 0; i < N; i++ {
            result[i] = (s0 + s1 + s2)
        }
        return unsafe.Pointer(&result)//<-- pointer of result
    }
    

    This cause a problem in Rules for passing pointers between Go and C.

    1. C code may not keep a copy of a Go pointer after the call returns.

    Source: https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md

    So you must convert unsafe.Pointer to uintptr golang type.

    func Function(s0, s1, s2 float64, N int) uintptr {
        result := make([]float64, N)
        for i := 0; i < N; i++ {
            result[i] = (s0 + s1 + s2)
        }
        return uintptr(unsafe.Pointer(&result[0]))//<-- note: result[0]
    }
    

    In this way you will work fine!

    Note: The structure of slice in C is represented by typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;, but C expect only data, for this is need result only void *data (first field, or field[0]).

    PoC: https://github.com/ag-studies/stackoverflow-pointers-ref-in-golang

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效