在Ruby中的Go通道

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

Go Channels in Ruby

问题

在Go编程语言中,您可以使用一种称为“通道”的构造来发送消息。
http://golang.org/doc/effective_go.html#channels

我希望在Ruby中使用类似的东西,特别是用于进程间通信。

我想要的伪代码:

channel = Channel.new

fork do
  3.times{ channel.send("foo ") }
  exit!
end

Thread.new do
  3.times{ channel.send("bar ") }
end

loop do
  print channel.recv
end

# ~> bar foo foo bar bar foo

是否有任何类似的构造、库或等效物可用于Ruby?

如果没有:构建这样一个抽象的最佳方法是什么?

**更新:**为了澄清我对这些通道的需求。

一个用例:一些分叉的工作进程等待任务。它们都从同一个JobChannel读取,并将结果报告给同一个ResultChannel。

我需要的通道

  • 非常快,
  • 写入不会阻塞,(消息发送)
  • 读取会阻塞,(消息接收)
  • 在分叉之前不需要特殊处理,
  • 轻量级和简单的实现会很好。

到目前为止,我尝试了以下方法

  • DRb,(与轻量级相反 + 速度慢 + 对于我来说有点太复杂)
  • Sockets,(UNIXSocket,TCPSocket ... Sockets似乎有很多使用方法。我在UNIXSockets上实现了一个半工作的通道。如果您认为套接字是有意义的,我应该关注哪些功能子集?)
  • Pipes,(连接超过2个进程似乎并不容易)

如果其中任何一种已经是解决我的问题的完美技术,请提供更多关于我的要求的重点信息的教程等。

英文:

In the Go programming language, you can send Messages around using a construct called "Channels".
http://golang.org/doc/effective_go.html#channels

I would love to use something like that in Ruby, especially for IPC.

Pseudocode of what I want:

channel = Channel.new

fork do
  3.times{ channel.send("foo ") }
  exit!
end

Thread.new do
  3.times{ channel.send("bar ") }
end

loop do
  print channel.recv
end

# ~> bar foo foo bar bar foo

Is there any construct, library or equivalent for Ruby which works like that ?

If not: What is the best way to build such an abstraction?

UPDATE: To clarify what I need from these Channels.

One use case: Some forked workers waiting for jobs. They all read from the same JobChannel and report results to the same ResultChannel.

The Channels I need

  • are very fast,
  • writes do not block, (message sending)
  • reads do block, (message receiving)
  • do not need special treatment before forking,
  • lightweight and simple would be nice.

So far I played around with

  • DRb, (opposite of lightweight + slow + too much magic for my little brain)
  • Sockets, (UNIXSocket, TCPSocket ... Sockets seem to have many many ways of using them. I got a half-working channel on UNIXSockets. If you think sockets make sense, what subset of features should I look at?)
  • Pipes. (Connecting more than 2 Processes seems to be non-trivial)

If any of those was already the perfect technology for my problem, please provide tutorials etc. which have more focused information on my requirements.

答案1

得分: 1

Go语言通过通道进行消息传递的概念,作为一种一流的构造,只有在并发存在的情况下才有意义(goroutines,tasklets,无论你愿意如何称呼它们)。有了廉价的并发,阻塞一个tasklet或coroutine不再是一个问题,阻塞的消息传递开始变得更有意义。

如果这是Python,我会向你推荐Stackless;在Ruby中,也许RevactorNeverBlock适合你的需求?

英文:

Go's idea of message passing via channels, as a first-class construct, really only makes sense in the presence of concurrency (goroutines, tasklets, whatever you'd care to call them). With cheap concurrency, blocking a tasklet or coroutine is no longer a problem, and blocking message passing starts to make a lot more sense.

If this were Python, I'd point you at Stackless; in Ruby, perhaps Revactor or NeverBlock fit the bill for you?

答案2

得分: 0

请查看这个问题:
在Ruby进程之间共享变量

还请查看drb

希望能对你有所帮助。

英文:

check out this question:
shared-variable-among-ruby-processes

and also check out drb

Hope it helps a bit.

答案3

得分: 0

请查看 agent gem,它的语法与您所需的非常接近:https://github.com/igrigorik/agent

英文:

Checkout agent gem, it has a syntax close to what you wnat: https://github.com/igrigorik/agent

答案4

得分: 0

Cod是一个使用通道进行进程间通信的宝石(gem)。

英文:

Cod is a gem for IPC using channels.

huangapple
  • 本文由 发表于 2010年6月17日 21:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/3062183.html
匿名

发表评论

匿名网友

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

确定