英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论