在Kubernetes中运行thread::sleep循环时出现奇怪的问题。

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

Weird issue with running thread::sleep in a loop in kubernetes

问题

我只能在我的牧场Kubernetes云中重现这个问题。在本地运行正常。然而,如果我创建一个调用循环中的sleep的新线程,Rocket会挂起,我不知道为什么。

tokio::spawn(async move {
  loop {
    thread::sleep(Duration::from_secs(1));
  }
});

这使得很难知道每隔x秒运行某个函数。

英文:

I am only able to reproduce this issue in my rancher kubernetes cloud. Locally it runs fine. However, if I spawn a new thread which calls sleep in a loop. Rocket will hang and I don't know why.

tokio::spawn(async move {
  loop {
    thread::sleep(Duration::from_secs(1));
  }
});

This makes it really hard to know run some function every x seconds.

答案1

得分: 1

使用 tokio::time::sleep,如 @aleksander-krauze 在评论中所解释的那样,修复了问题。

英文:

using tokio::time::sleep instead as explained by @aleksander-krauze in the comments fixed the issue.

huangapple
  • 本文由 发表于 2023年6月6日 04:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409934.html
匿名

发表评论

匿名网友

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

确定