Crystal Crystal 1.0.0 Released!

ThxFly · 2021年03月23日 · 最后由 qq2729877005 回复于 2021年04月06日 · 1459 次阅读

新的里程碑

@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

steve 回复

不清楚哦,fiber 还没用到过

steve 回复

用 channel 就好了

channel = Channel(Nil).new()

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

channel.receive
lidashuang 回复

心动了

生态是硬伤😂

需要 登录 后方可回复, 如果你还没有账号请 注册新账号