Simplest pub-sub for golang <–> python communication, possibly across machines?

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

Simplest pub-sub for golang <--> python communication, possibly across machines?

问题

我正在使用Golang编写一个Web应用程序,需要调用一个Python程序/模块来执行一些繁重的工作。由于这些工作对内存和CPU要求很高,可能需要在另一台机器上执行。由于Golang和Python不能直接通信,有三种方法可以实现这一点:

  1. 从Go中作为操作系统进程执行Python程序(如果在同一台机器上)(或者使用RPC?)
  2. 将Python进程封装为一个服务,并公开供Go调用(可以是一个简单的CRUD服务,如Bottle/flask restful服务)
  3. 使用简单的发布-订阅系统来实现(如Redis或某个消息队列系统)- 添加基于Redis的缓存是计划中的,所以这可能是选择这种方式的一个好理由。不确定。

最重要的是,执行时间很长的Python进程必须“通知”Web应用程序已经完成。数据可以存储在文件/数据库中,或者由进程“返回”。

在发布-订阅环境中,最简单的实现方式是什么?

更新
REST似乎是一种方式,但会增加实现服务器端推送的成本,这可能在现有的微型Web框架中可能不容易实现。发布-订阅模式会增加额外的复杂性和学习曲线。我不确定是否可以在不同机器之间实现类似RPC的调用。在这方面,有什么好的选择吗?

英文:

I'm working on a web application written in Golang that needs to call a Python program/module to do some heavy work. Since that is very memory/CPU intensive, it may be on a separate machine. Since Golang and Python can't talk directly, there are 3 ways to achieve this:

  1. Just execute the python program as an OS process from Go (if on same machine) (or RPC?)
  2. Wrap Python process in a service and expose it for it to be called from Go (may be a simple CRUD like service - A Bottle/flask restful service)
  3. Have a simple pub-sub system in place to achieve this (Redis or some MQ system) - Adding Redis based caching is on the radar so maybe a good reason to go this way. Not sure.

The main thing is that the python process that takes really long to finish must "inform" the web application that it has finished. The data could either be in a file/DB or 'returned' by the process.

What could be the simplest way to achieve this in a pub/sub like environment?

UPDATE
REST seems like one way but would incur the cost of implementing server side push which may or may not be easily doable with existing micro web frameworks. The pub/sub would add an additional layer of complexity for maintainability and a learning curve nevertheless. I'm not sure if an RPC like invocation could be achieved across machines. What would be a good choice in this regard?

答案1

得分: 4

使用 ZeroMQ

Python 有一个很好的 pyzmq 库。

GO 绑定 也存在。

ZeroMQ 的代码通常很简短而有效。

你甚至可以在 Unix 上使用本地套接字。

除了发布/订阅之外,还有其他的消息传递模式,比如请求/响应或推送/拉取。

不了解你的应用程序,但我可以提供一个在 Python 中使用 ZeroMQ 集成锁管理器的示例代码:在 Python 中使用 ZeroMQ 集成锁管理器

使用 ZeroRPC

有一个实验性的 Golang 库,可以通过 ZeroMQ 与 Python 和 Node.js 代码进行通信,示例代码如下(适用于 Python 和 Node.js):zerorpc.dotclould.com

在 Python 中编写可通过 ZeroRPC 调用的函数非常简单 - 实际上,你不需要编写任何使用 zeromq 的代码(有一个命令行工具,允许将函数与正确形状的签名集成)。

英文:

Use ZeroMQ

Python has nice pyzmq

GO bindngs exists too.

ZeroMQ code is typically short and effective.

You can even use local socket on Unix.

There are even other messaging patterns apart from pub/sub, you can use req/resp or push/pull.

Not knowing much about your applications, I can offer sample of integrating lock manager using zeromq in Python

Using ZeroRPC

There is experimental Golang lib being able communicating with Python and Node.js code via ZeroMQ as shown (for Python and Node.js) zerorpc.dotclould.com

Writing functions in Python being callable over ZeroRPC is extremely simple - in fact you do not have to write a line using zeromq (there is command line tool, which allows integrating functions with properly shaped signatures).

答案2

得分: 0

对于你的特定模式,从Go中生成进程并读取stdout是最高效的,没有必要增加额外的开销。

这非常依赖于你的Python脚本的功能,如果它只是一个特定的任务,那么简单地生成进程并检查退出代码就足够了;如果你必须一直在后台运行脚本并与之通信,那么Redis或ZeroMQ都是不错的选择,而且在Go和Python上都非常成熟。

如果它在不同的服务器上,那么ZeroMQ/RPC或者只是一个简单的Python HTTP服务器都可以,开销应该很小。

英文:

For your specific pattern, simply spawning the process from Go and reading the stdout is the most efficient, there's no point adding an over head.

It highly highly depends on what your python script does, if it's one specific task then simply spawning the process and checking the exit code is more than enough, if you have to keep the script in the background at all time and communicate with it then Redis or ZeroMQ are good, and very mature on both Go and Python.

If it's on a different server then ZeroMQ/RPC or just a plain http server in python should be fine, the overhead should be minimal.

huangapple
  • 本文由 发表于 2014年7月17日 05:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/24791113.html
匿名

发表评论

匿名网友

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

确定