Ruby多线程与多进程示例

多进程示例:

multiprocess.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def test_proc(name,second)
Process.setproctitle("PROC-#{Process.pid}" )
sleep second
puts "#{name} - #{Process.pid} |G/U:#{Process.euid}/#{Process.gid}|PPID:#{Process.ppid}"
Process.exit
puts "Never going here"
end

puts "Main process start - #{Process.pid}"

10.times do |t|

Process.fork do
test_proc("PROC-#{t}",5+t)
end

end

#Process.exit
Process.waitall
puts "Main process exit - #{Process.pid}"

运行结果:

多线程示例:

multithread.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14

require "thread"

threads=[]
6.times do |t|
threads <<Thread.new do
puts "Thread-#{t} start: Process: #{Process.pid} "
sleep 5
puts "Thread-#{t} Done: Process: #{Process.pid} "
end
end

threads.each{|t|t.join}

运行结果:

Dockerfile一例:搭建DVWA环境 Python 3 多线程与多进程示例
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×