julia – 更好的等待 @async 循环方式

huangapple go评论55阅读模式
英文:

julia - Better way to wait on @async loop

问题

以下是您要翻译的内容:

这个异步循环将立即进入下一行。

在没有条件需要等待时,最佳方式是什么?

@async while true
    sleep(1)
end

# 必须是异步,否则第一个循环将永远没有机会运行
@async start_server_loop()

exit = Condition()
wait(exit)
英文:

This async loop will immediately go to the next line.

What is the best way to wait when there is no need for a condition?

@async while true
    sleep(1)
end

# Must be async or first loop will never get a chance to run
@async start_server_loop()

exit = Condition()
wait(exit)

答案1

得分: 1

假设您的目标与注释中的一样:“我只想永远等待,同时让其他循环继续运行”:

@sync begin

  @async do_loop1()
  @async do_loop2()
  @async do_loop3()

end

请注意,此代码仅在循环在I/O上阻塞时才有用。如果循环需要大量CPU资源,您应该使用线程而不是协程。

英文:

Assuming that your goal is as in the comment "I just want to wait forever while letting the other loops do their thing":

@sync begin

  @async do_loop1()
  @async do_loop2()
  @async do_loop3()

end

Note that this code is only useful when the loops block on I/O. If the loops are CPU-intensive you should use threads instead.

答案2

得分: 1

It sounds like you want to suspend the root-task.
这听起来像是你想要暂停根任务。

Just wait() will do that.
只需使用 wait() 即可。

If no argument is passed, the task blocks for an undefined period. A task can only be restarted by an explicit call to schedule or yieldto.
如果没有传递参数,任务将会阻塞一段未定义的时间。只有通过显式调用 schedule 或 yieldto 才能重新启动任务。

Alternatively you could call start_server_loop() and execute that on the root-task itself. (Or wait(@async start_server_loop()))
或者你可以调用 start_server_loop() 并在根任务上执行它。(或者 wait(@async start_server_loop())

# Must be async or first loop will never get a chance to run
必须是异步的,否则第一个循环永远不会有机会运行。

This sounds like you are running into another issue. Julia is using cooperative tasking. That means the runtime will not interrupt a task while it is running. So your start_server_loop() or your while true; ...; end loop need an explicit call to yield() in order to give other tasks time to run.
这听起来像是你遇到了另一个问题。Julia 使用协作式任务处理。这意味着运行时在任务运行时不会中断任务。因此,你的 start_server_loop()while true; ...; end 循环需要显式调用 yield() 以便让其他任务有机会运行。

Lastly, I would recommend using Threads.@spawn so that you can use multiple worker threads to execute your tasks. @async will execute the code on the same thread it was spawned from.
最后,我建议使用 Threads.@spawn,这样你可以使用多个工作线程来执行任务。@async 将在生成它的相同线程上执行代码。

英文:

It sounds like you want to suspend the root-task.

Just wait() will do that.

> If no argument is passed, the task blocks for an undefined period. A task
can only be restarted by an explicit call to schedule or yieldto.

Alternatively you could call start_server_loop() and execute that on the root-task itself. (Or wait(@async start_server_loop()))

> # Must be async or first loop will never get a chance to run

This sounds like you are running into another issue. Julia is using cooperative tasking. That means the runtime will not interrupt a task, while it is running. So your start_server_loop() or your while true; ...; end loop need an explicit call to yield() in order to give other tasks time to run.

Lastly I would recommend using Threads.@spawn so that you can use multiple worker threads to execute your tasks. @async will execute the code on the same thread it was spawned from.

huangapple
  • 本文由 发表于 2023年5月30日 09:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定