如何运行多个ruby守护进程并处理每个守护进程的输入和输出?
这是代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您的意思是您希望同时运行多个 Ruby 代码。您可以使用 Ruby 线程(有自己的 陷阱)或者您可以使用操作系统的作业控制工具。如果您使用的是 UNIX-y,您可以将每个守护程序的代码放在单独的 .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.,
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.
忽略您认为多个守护进程同时争夺 STDIN 会发生的情况:
这将创建三个无限循环的线程,请求输入然后输出它。每个线程都是“连接”的,防止主程序退出,直到每个线程完成(但永远不会)。
Ignoring what you think will happen with multiple daemons all fighting over STDIN at the same time:
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).
您可以尝试使用 multi_daemons gem,它能够运行多个守护进程并控制它们。
启动和停止
You could try using multi_daemons gem which has capability to run multiple daemons and control them.
To start and stop