在 Rails 中使用命名管道

发布于 2024-08-26 16:37:52 字数 239 浏览 3 评论 0原文

我需要通过命名管道读取和写入一些数据。

我已经在一个简单的 Ruby 应用程序中对其进行了测试,效果很好。

但我不知道,我应该把它放在 Rails 应用程序的哪里?我听说过 Rake 任务,但我不确定这是否是正确的解决方案。

我需要打开一个管道文件,并监听数据。如果有的话,我需要处理它并进行数据库查询。然后,将一些数据写入另一个管道。我知道它是如何工作的,但唯一的问题是如何用 Rails 运行它?请给我一些例子。

I need to read and write some data through named pipes.

I have tested it in a simple Ruby app, and it works nice.

But I dont know, where should i put it in my Rails app? I have heard about Rake tasks, but i don't sure, is it right solution.

I need to open a pipe-file, and listen to data. If there is any, i need to process it and make a DB-query. Then, write some data to another pipe. I know, how it works, but the only problem - how to run it with Rails? Give me some examples, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2024-09-02 16:37:52

听起来您有一个网站,该网站将具有通过管道流式传输的后端数据源。听起来这也不会成为 HTTP 请求/响应周期的一部分,这可能使 rake 任务成为一个不错的选择。

在 lib/tasks 中创建一个名为 Listener.rake 的文件,

它应该如下所示:

desc 'Listens to pipe and does stuff'
task :listen_to_pipe => :environment do
  puts "Listen to Pipe starting"
  #open pipe
  #loop to listen to it
    puts "going to do stuff"
    #do stuff
  #end
end

然后,从项目根目录中的命令行,您可以像这样调用它:

rake listen_to_pipe

对于不同的环境,执行以下操作:

rake listen_to_pipe RAILS_ENV=production

此任务将具有访问您的所有模型。要结束它,请按 Ctrl+C

请记住,您需要停止并停止它。重新启动进程以加载对模型所做的任何更改。

It sounds like you have a website that will have a backend source of data that you are streaming through a pipe. It also sounds like this is not going to be part of the HTTP Request/Response cycle, which could make a rake task a good choice.

Make a file in lib/tasks called listener.rake

it should look like this:

desc 'Listens to pipe and does stuff'
task :listen_to_pipe => :environment do
  puts "Listen to Pipe starting"
  #open pipe
  #loop to listen to it
    puts "going to do stuff"
    #do stuff
  #end
end

Then, from the command line in the root dir of your project you can invoke it like this:

rake listen_to_pipe

and for a different environment, do this:

rake listen_to_pipe RAILS_ENV=production

This task will have access to all your models. To end it, hit Ctrl+C

Keep in mind you will need to stop & restart the process to load any changes made to models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文