如何解决如何从 go to python 返回 mongodb 的文档
我正在尝试使用 golang 扩展我的 python 代码以使用 goroutines。我想从多个 goroutine 中的 mongo 获取文档并将其发送回 python 模块。现在我的主要问题是在 go 和 python 之间转换数据类型。例如,我如何将 fetchModelInstances 结果发送到 python?
main.py
from ctypes import *
from pathlib import Path
here = Path(__file__).absolute().parent
so = cdll.LoadLibrary(str(here / "fetch.so"))
fetchObjects = so.fetchObjects
p = fetchObjects()
main.go
package main
import "C"
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
//export fetchObjects
func fetchObjects() {
ids := []int{86,971526}
fetchModelInstances(ids)
}
func fetchModelInstances(value []int) []bson.M {
ctx,cancel := context.WithTimeout(context.Background(),10*time.Second)
defer cancel()
client,_ := mongo.Connect(ctx,options.Client().ApplyURI("mongodb://localhost:27017"))
collection := client.Database("blog").Collection("post")
cur,_ := collection.Find(ctx,bson.M{"_id": bson.M{"$in": value}})
var res []bson.M
err := cur.All(ctx,&res)
if err != nil {
fmt.Println(err.Error())
}
return res
}
func main() {}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。