thread, fiber, continuation
不過可直接用...如程式中直接sleep...
不用透過thread去start
<code> while true do
sleep 1;
puts "take a break" //我沒有廣告嫌疑....
end</code>
我... 還是直接看範例吧
<code>f=Fiber.new{
puts "Fiber says Hello"
Fiber.yield
puts "Fiber says Goodbye"
}
puts "Caller says Hello"
f.resume
puts "Caller says Goodbye"
f.resume</code>
Output
Caller says Hello
Fiber says Hello
Caller says Goodbye
Fiber says Goodbye
reference: The Ruby Programmikng Language
方法、proc、lambda與colsure
反射 reflection
延續伺服器 continuation server
<code>def loop
for i in 1..5 do
puts i
callcc {|continualtion| return continuation} if i= =3
end # cont.call returns here
return nil
end
puts "Before loop call"
cont=loop()
puts "After loop call"
cont.call if cont
puts "After continuation call"</code>
結果
Before loop call
1
2
After loop call
3
4
5
After loop call
After continuation call
何時用
多部電腦之間同步的「請求 - 回應」溝通時
主線需暫停控制權,直到遠端系統回應之後再繼續
=>方便解決Http是stateless問題,不需用到session
ex. seaside web framework
看不懂說什麼嗎...
我也看不懂...
- thread
不過可直接用...如程式中直接sleep...
不用透過thread去start
<code> while true do
sleep 1;
puts "take a break" //我沒有廣告嫌疑....
end</code>
- fiber
我... 還是直接看範例吧
<code>f=Fiber.new{
puts "Fiber says Hello"
Fiber.yield
puts "Fiber says Goodbye"
}
puts "Caller says Hello"
f.resume
puts "Caller says Goodbye"
f.resume</code>
Output
Caller says Hello
Fiber says Hello
Caller says Goodbye
Fiber says Goodbye
reference: The Ruby Programmikng Language
- continuation
方法、proc、lambda與colsure
反射 reflection
延續伺服器 continuation server
<code>def loop
for i in 1..5 do
puts i
callcc {|continualtion| return continuation} if i= =3
end # cont.call returns here
return nil
end
puts "Before loop call"
cont=loop()
puts "After loop call"
cont.call if cont
puts "After continuation call"</code>
結果
Before loop call
1
2
After loop call
3
4
5
After loop call
After continuation call
何時用
多部電腦之間同步的「請求 - 回應」溝通時
主線需暫停控制權,直到遠端系統回應之後再繼續
=>方便解決Http是stateless問題,不需用到session
ex. seaside web framework
看不懂說什麼嗎...
我也看不懂...
沒有留言:
張貼留言