How to structure golang api that uses function from python file

huangapple go评论97阅读模式
英文:

How to structure golang api that uses function from python file

问题

我正在编写一个API,将通过Websockets发送消息。

我有一个Python监控函数,用于监控和格式化将通过Websockets发送的数据。如果这个监控函数是用Golang编写的,我会在一个单独的goroutine上运行该函数,并且每当函数获取到新数据时,我会通过一个通道将其发送,并通过Websockets连接发送。

如果监控函数是一个Python函数,是否有可能实现类似的行为?考虑到我的使用情况,我不想使用gRPC,因为速度很重要。

英文:

I am writing an api that will be sending messages over websockets.

I have a python monitoring function that is used to monitor and format the data which will be sent over the websocket. If this monitoring function was written in golang, I'd run the function on a separate goroutine and each time new data was fetched by the function I would send it through a channel and then send it over the websocket connection.

How/is it possible to achieve a similar type of behaviour if the monitoring function is a python function? I'd rather not use grpc as speed is important for my use case

答案1

得分: 2

如果您想要使用Python函数来监控和格式化Go程序中的数据,您可以使用CPython API的任何实现(例如cpy3)来从Go程序中调用Python函数。例如,gpython使用CPython API将Python嵌入到Go中。以下是一个示例,展示了如何使用这样的API来监控Python函数。

package main

// #cgo CFLAGS: -I/usr/include/python3.8
// #cgo LDFLAGS: -lpython3.8
// #include <Python.h>
import "C"
import (
	"fmt"
	"unsafe"
)

func main() {
	C.Py_Initialize()
	defer C.Py_Finalize()

	pyName := C.CString("monitor_module")
	defer C.free(unsafe.Pointer(pyName))
	pyModule := C.PyImport_ImportModule(pyName)
	if pyModule == nil {  // 处理错误 }

	pyFunc := C.PyObject_GetAttrString(pyModule, C.CString("monitor_function"))
	pyArgs := C.PyTuple_New(0)
	pyResult := C.PyObject_CallObject(pyFunc, pyArgs)

	result := C.GoString(C.PyUnicode_AsUTF8(pyResult)) // 将Python结果转换为Go字符串
	fmt.Println(result)
}

希望对您有所帮助!

英文:

If you want to use a Python function to monitor and format data in a Go program, you can use any of the implementation of the CPython API (e.g., cpy3) to call Python functions from a Go program. gpython, e.g., uses the CPython API to embed Python in Go. Here's na example of how you can use one such API for monitoring functions of Python.

package main

// #cgo CFLAGS: -I/usr/include/python3.8
// #cgo LDFLAGS: -lpython3.8
// #include &lt;Python.h&gt;
import &quot;C&quot;
import (
	&quot;fmt&quot;
	&quot;unsafe&quot;
)

func main() {
	C.Py_Initialize()
	defer C.Py_Finalize()

	pyName := C.CString(&quot;monitor_module&quot;)
	defer C.free(unsafe.Pointer(pyName))
	pyModule := C.PyImport_ImportModule(pyName)
	if pyModule == nil {  // handle error  }

	pyFunc := C.PyObject_GetAttrString(pyModule, C.CString(&quot;monitor_function&quot;))
	pyArgs := C.PyTuple_New(0)
	pyResult := C.PyObject_CallObject(pyFunc, pyArgs)

	result := C.GoString(C.PyUnicode_AsUTF8(pyResult)) // Python result to a Go string
	fmt.Println(result)
}

huangapple
  • 本文由 发表于 2023年5月6日 03:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76185208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定