如何运行多个ruby守护进程并处理每个守护进程的输入和输出?

发布于 2024-11-19 02:35:10 字数 1102 浏览 5 评论 0原文

这是代码:

while 1
    input = gets
    puts input
end

这是我想做的,但我不知道该怎么做: 我想创建多个在后台运行的代码实例,并能够将输入传递到特定实例。

Q1:如何在后台运行脚本的多个实例?

问题 2:如何引用脚本的单个实例,以便将输入传递给该实例(问题 3)?

Q3:脚本使用 cmd“gets”来获取输入,我如何将输入传递到个人脚本的 gets 中?

例如,

假设我在后台运行代码的三个实例,我分别将这些实例称为#1、#2 和#3。 我将“hello”传递给#1,#1 将“hello”显示到屏幕上。 然后我将“world”传递给#3,#3 将“hello”显示到屏幕上。

谢谢!

更新: 回答了我自己的问题。找到了这个很棒的图:http://rubylearning.com/satishtalim/ruby_threads.html和资源在这里: http://www.ruby-doc.org/core/classes/Thread.html#M000826

puts Thread.main

x = Thread.new{loop{puts 'x'; puts gets; Thread.stop}}
y = Thread.new{loop{puts 'y'; puts gets; Thread.stop}}
z = Thread.new{loop{puts 'z'; puts  gets; Thread.stop}}

while x.status != "sleep" and y.status != "sleep" and z.status !="sleep"
    sleep(1)
end

Thread.list.each {|thr| p thr }

x.run
x.join

谢谢大家的帮助!帮助澄清我的想法。

Here's the code:

while 1
    input = gets
    puts input
end

Here's what I want to do but I have no idea how to do it:
I want to create multiple instances of the code to run in the background and be able to pass input to a specific instance.

Q1: How do I run multiple instances of the script in the background?

Q2: How do I refer to an individual instance of the script so I can pass input to the instance (Q3)?

Q3: The script is using the cmd "gets" to take input, how would I pass input into an indivdual's script's gets?

e.g

Let's say I'm running threes instances of the code in the background and I refer to the instance as #1, #2, and #3 respectively.
I pass "hello" to #1, #1 puts "hello" to the screen.
Then I pass "world" to #3 and #3 puts "hello" to the screen.

Thanks!

UPDATE:
Answered my own question. Found this awesome tut: http://rubylearning.com/satishtalim/ruby_threads.html and resource here: http://www.ruby-doc.org/core/classes/Thread.html#M000826.

puts Thread.main

x = Thread.new{loop{puts 'x'; puts gets; Thread.stop}}
y = Thread.new{loop{puts 'y'; puts gets; Thread.stop}}
z = Thread.new{loop{puts 'z'; puts  gets; Thread.stop}}

while x.status != "sleep" and y.status != "sleep" and z.status !="sleep"
    sleep(1)
end

Thread.list.each {|thr| p thr }

x.run
x.join

Thank you for all the help guys! Help clarified my thinking.

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

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

发布评论

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

评论(3

云裳 2024-11-26 02:35:10

我假设您的意思是您希望同时运行多个 Ruby 代码。您可以使用 Ruby 线程(有自己的 陷阱)或者您可以使用操作系统的作业控制工具。如果您使用的是 UNIX-y,您可以将每个守护程序的代码放在单独的 .rb 文件中并同时运行它们。

例如,

# ruby daemon1.rb &
# ruby daemon2.rb &

Ruby 程序中有多种“处理输入和输出”的方法。管道、套接字等。既然您询问了守护进程,我假设您指的是网络 I/O。请参阅 Net::HTTP

I assume that you mean that you want multiple bits of Ruby code running concurrently. You can do it the hard way using Ruby threads (which have their own gotchas) or you can use the job control facilities of your OS. If you're using something UNIX-y, you can just put the code for each daemon in separate .rb files and run them at the same time.

E.g.,

# ruby daemon1.rb &
# ruby daemon2.rb &

There are many ways to "handle input and output" in a Ruby program. Pipes, sockets, etc. Since you asked about daemons, I assume that you mean network I/O. See Net::HTTP.

梦在深巷 2024-11-26 02:35:10

忽略您认为多个守护进程同时争夺 STDIN 会发生的情况:

(1..3).map{ Thread.new{ loop{ puts gets } } }.each(&:join)

这将创建三个无限循环的线程,请求输入然后输出它。每个线程都是“连接”的,防止主程序退出,直到每个线程完成(但永远不会)。

Ignoring what you think will happen with multiple daemons all fighting over STDIN at the same time:

(1..3).map{ Thread.new{ loop{ puts gets } } }.each(&:join)

This will create three threads that loop indefinitely, asking for input and then outputting it. Each thread is "joined", preventing the main program from exiting until each thread is complete (which it never will be).

守望孤独 2024-11-26 02:35:10

您可以尝试使用 multi_daemons gem,它能够运行多个守护进程并控制它们。

# this is server.rb

proc_code = Proc do
  loop do
    sleep 5
  end
end

scheduler = MultiDaemons::Daemon.new('scripts/scheduler', name: 'scheduler', type: :script, options: {})
looper = MultiDaemons::Daemon.new(proc_code, name: 'looper', type: :proc, options: {})
MultiDaemons.runner([scheduler, looper], { force_kill_timeout: 60 })

启动和停止

ruby server.rb start
ruby server.rb stop

You could try using multi_daemons gem which has capability to run multiple daemons and control them.

# this is server.rb

proc_code = Proc do
  loop do
    sleep 5
  end
end

scheduler = MultiDaemons::Daemon.new('scripts/scheduler', name: 'scheduler', type: :script, options: {})
looper = MultiDaemons::Daemon.new(proc_code, name: 'looper', type: :proc, options: {})
MultiDaemons.runner([scheduler, looper], { force_kill_timeout: 60 })

To start and stop

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