python - AttributeError : dlsym(0x7fc4cfd563b0, add_all_items_to_map): symbol not found;使用 C 从 Python 运行 Go

标签 python go shared-libraries

我有以下go文件:

//try_async.go
package main

import (
    "C"
    "fmt"
    "math/rand"
    "sync"
    "time"
)

var mutex sync.Mutex
var wg sync.WaitGroup

func random_sleep() {
    r := rand.Intn(3000)
    time.Sleep(time.Duration(r) * time.Millisecond)
}


func add_to_map(m map[string] string, word string) {
    defer wg.Done()
    added_word := word + " plus more letters"
    fmt.Println("Before sleep")
    random_sleep()
    mutex.Lock()
    defer mutex.Unlock()
    m[word] = added_word
    fmt.Println("Added word %v", word)
}

// export add_all_items_to_map
func add_all_items_to_map(words []string) map[string]string {
    words_map := make(map[string]string)
    for _, this_word := range words {
        wg.Add(1)
        go add_to_map(words_map, this_word)
    }
    wg.Wait()
    return words_map
}


func main() {
    // result := add_all_items_to_map([]string{"cat", "dog", "fish"})
    // fmt.Println(result)
}

我有 Python 脚本:

from ctypes import cdll

"""
run

  go build -buildmode=c-shared -o try_async.so try_async.go

first
"""

lib = cdll.LoadLibrary('./try_async.so')

print("Loaded go lib")
result = lib.add_all_items_to_map(['cat', 'dog', 'fish'])
print(result)

结果是奇怪的错误,别人说的是你没有构建共享对象,但我做了:

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ rm *.so

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ go build -buildmode=c-shared -o try_async.so try_async.go

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ python go-async-caller.py 
Loaded go lib
Traceback (most recent call last):
  File "go-async-caller.py", line 14, in <module>
    result = lib.add_all_items_to_map(['cat', 'dog', 'fish'])
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 375, in __getattr__
    func = self.__getitem__(name)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 380, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7fc4cfd563b0, add_all_items_to_map): symbol not found

以下确实有效:

libadd.go-

//libadd.go
package main

import "C"

//export add
func add(left int, right int) int {
    return left + right
}

func main() {}

go-caller-example.py-

from ctypes import cdll

"""
run

  go build -buildmode=c-shared -o libadd.so libadd.go

first
"""

lib = cdll.LoadLibrary('./libadd.so')

print("Loaded go lib")
result = lib.add(2, 3)
print(result)

像这样

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ go build -buildmode=c-shared -o libadd.so libadd.go

cchilders:~/work_projects/golang_integration (feature/golang-query) 
$ python go-caller-example.py 
Loaded go lib
5

http://savorywatt.com/2015/09/18/calling-go-code-from-python-code/

最佳答案

Go 中的指令有点脆弱,你需要让它们完全正确,否则它们会悄无声息地失败。

在这种情况下你有这个:

// export add_all_items_to_map

但需要这个:

//export add_all_items_to_map

参见 the cgo docs获取更多信息。

关于python - AttributeError : dlsym(0x7fc4cfd563b0, add_all_items_to_map): symbol not found;使用 C 从 Python 运行 Go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381104/

相关文章:

python - 在pytorch中将矩阵行乘以向量元素?

encryption - 使 GCM/CBC 密码在 golang 中流式传输

go - 如何在包括内部软件包的整个项目上运行“go vet”?

c - 在 C 编程中访问共享库函数时如何修复段错误?

python - ModuleNotFoundError : No module named 'plotly.graph_objects'

python - TQDM 和多处理 - python

python - 如何在 mechanize 中单击表单中的标签?

go - 使用 aws-sdk-go 将对象上传到 AWS S3 而无需创建文件

c - 如何隐藏在多个文件中可见的全局变量?

C++ 动态库编译/链接