Crystal Crystal 1.0.0 Released!

ThxFly · March 23, 2021 · Last by qq2729877005 replied at April 06, 2021 · 1459 hits

新的里程碑

@ThxFly 楼主很熟悉 crystal,请教个 fiber 的问题

require "crest"

module FiberTest
  VERSION = "0.1.0"

  spawn do
    puts "开始执行"
    body = Crest.get("https://ruby-china.org/").body
    puts body.size
    puts "结束执行"
  end

  Fiber.yield
end

输出:开始执行

主线程里把执行机会给 fiber,fiber 执行到网络 io 后也放弃执行机会,调度回到主线程,然后程序退出。 有办法让 fiber 把 block 里的代码都执行完吗?

似乎只能用 channel

Reply to steve

不清楚哦,fiber 还没用到过

Reply to steve

用 channel 就好了

channel = Channel(Nil).new()

spawn do
    sleep 3
    puts "fiber done."
    channel.send(nil)
end

channel.receive
Reply to lidashuang

心动了

生态是硬伤😂

You need to Sign in before reply, if you don't have an account, please Sign up first.