简体   繁体   English

混合Python和Go

[英]Mixing Python and Go

I have been working on a library in Python and I would like to do some performance improvement. 我一直在使用Python的库,我想做一些性能改进。

Is it possible to write some code in Python and some code in Go, and pass data between them? 是否可以在Python中编写一些代码,在Go中编写一些代码,并在它们之间传递数据? And if it's possible, are there any examples on how to do this? 如果有可能,有没有关于如何做到这一点的例子?

Like such: 像这样:

# Python
def python_foo():
    data = {'foo': 'val', 'bar': [1, 2, 3]}
    go_process(json.dumps(data))


def python_got_data_from_go(data):
    # deal with data from Go


# Go
func go_process(json string) {
    // do some processing
    python_got_data_from_go(someData)
}

You need a glue between them, for example C programming language or communication over network. 您需要在它们之间粘合 ,例如C编程语言或通过网络进行通信。 Most painful solution if you mix https://docs.python.org/2/extending/extending.html with http://golang.org/cmd/cgo/ and good programming skills in C . 如果你将https://docs.python.org/2/extending/extending.htmlhttp://golang.org/cmd/cgo/C良好编程技巧相结合,那么最痛苦的解决方案。

You might create server in python http://pymotw.com/2/socket/tcp.html and in go https://coderwall.com/p/wohavg and communicate between them. 您可以在python http://pymotw.com/2/socket/tcp.html中创建服务器,然后转到https://coderwall.com/p/wohavg并在它们之间进行通信。

Edit: see Writing a Python extension in Go (Golang) and Calling Python function from Go and getting the function return value 编辑:请参阅在Go(Golang)中编写Python扩展从Go调用Python函数并获取函数返回值

The easiest way to mix languages is to to communicate over a socket, such as TCP/IP or Unix domain socket or through a high level protocol like HTTP or XML-RPC. 混合语言的最简单方法是通过套接字(如TCP / IP或Unix域套接字)或通过HTTP或XML-RPC等高级协议进行通信。 This comes with very high overhead though due to the request processing and serialization/deserialization to/from JSON/XML, which can be significant if you have to make lots of calls. 由于JSON / XML的请求处理和序列化/反序列化,这会带来非常高的开销,如果您需要进行大量调用,这可能很重要。 Communicating over socket is usually best if the amount of workload per request is high. 如果每个请求的工作负载量很高,通过套接字进行通信通常是最好的。

If you are not willing to pay for the overhead of socket (say if you make thousands of requests back and forth between python and go per seconds), there are other solutions that may have lower overhead. 如果你不愿意支付套接字的开销(比如你在python和每秒之间来回发出数千个请求),那么其他解决方案可能会有更低的开销。 You may be able to use shared memory in OSes that have them. 您可以在拥有它们的操作系统中使用共享内存。 Shared memory usually incurs much cheaper cost to access data, but it may incur boxing/unboxing cost from the shared memory structure to Python datatypes. 共享内存通常会导致访问数据的成本更低,但它可能会导致从共享内存结构到Python数据类型的装箱/拆箱成本。 Also, note that you may have to manage the locks yourself with this. 另请注意,您可能需要自行管理锁。

Unless you only make very small number of calls and they do not need to share state between calls I would not recommend communicating using the standard stdin/stdout. 除非您只进行非常少量的呼叫,并且他们不需要在呼叫之间共享状态,否则我不建议使用标准的stdin / stdout进行通信。

The last alternative is to write Python Extension; 最后一种方法是编写Python扩展; I would not recommend this for the faint of heart. 对于胆小的人,我不建议这样做。

The simplest solution is to spawn a Go process from Python and make them communicate via the Go's process standard streams ( os.Stdin and os.Stdout ). 最简单的解决方案是从Python生成Go进程,并通过Go的进程标准流( os.Stdinos.Stdout )进行通信。 You have to invent a protocol both sides agree on (looks like you've settled on JSON already) and not to forget to flush the stream after streaming a logically atomic request, from both ends. 你必须发明一个双方都同意的协议(看起来你已经确定了JSON)并且不要忘记在从两端流式传输逻辑原子请求后刷新流。

This way your solution is (almost) cross-platform and very easy to set up. 这样,您的解决方案(几乎)跨平台,并且非常容易设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM