停止一个未知函数在5秒后运行。

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

Stop an unknown function from running after 5 seconds

问题

以下是翻译好的部分:

"Let's say I have a function someFunction() that I don't have any control of what's inside of it.
I wish to run this function for no more than 5 seconds.

I've tried using a setTimeout or setInterval like this:

try {
   const timeoutId = setTimeout(() => {
      throw new Error("Time over");
   }, 5000);

   someFunction();
   clearTimeout(timeoutId);
} catch (e) {
   ...
}

The problem in this is that if there is an infinite loop in someFunction(), then the timeout will never get called.

What's the simplest way to solve this? I thought about using a worker thread, but passing arguments to another thread is problematic in my case.

Thanks a lot!"

英文:

Let's say I have a function someFunction() that i don't any control of what inside of it.
I wish to run this function for no more than 5 seconds.

I've tried using a setTimeout or setInterval like this:

try {
   const timeoutId = setTimeout(() => {
      throw new Error("Time over");
   }, 5000);

   someFunction();
   clearTimeout(timeoutId);
} catch (e) {
   ...
}

The problem in this is that if there is an infinite loop in someFunction() then the timeout will never get called.

what's the simplest way to solve this? I thought about using a worker thread but passing arguments to another thread is problematic in my case.

Thanks a lot!

答案1

得分: 6

下面是翻译好的部分:

"what's the simplest way to solve this?"
"这个问题的最简单解决方法是什么?"

"You can't, within the same execution environment. If you don't control what's inside the function, there's no way for you to stop it from your code. Once you've made the call to someFunction, your code doesn't get control back until that function has run to completion."
"在相同的执行环境中,你无法做到这一点。如果你无法控制函数内部的内容,就没有办法通过你的代码停止它。一旦你调用了 someFunction,你的代码在该函数完成运行之前无法恢复控制权。"

"You could spawn a worker thread or child process that runs the function in a separate environment, which you can then terminate (1, 2) after a period of time. But within your main environment, you can't do it."
"你可以创建一个工作线程子进程,在一个独立的环境中运行该函数,然后在一段时间后终止它(12)。但在你的主要执行环境中,你无法做到这一点。"

"Note that in the case of an async function, "run(s) to completion" means "reaches the point where it returns its promise" (the first await, return [implicit or explicit], or uncaught error). So in theory if you had an async function that started something then didn't go into the finite loop until after the first await (say), your code would get an opportunity to run, but if you have no control over the function's code there's nothing you can do from keeping it from continuing again (other than run an infinite loop of your own, which of course you wouldn't want to do)."
"请注意,在async函数的情况下,“运行完成”意味着“达到返回其Promise的点”(第一个awaitreturn[隐式或显式]或未捕获的错误)。因此,理论上,如果你有一个async函数,它开始执行某些操作,然后在第一个await之后才进入有限循环(例如),你的代码将有机会运行。但如果你无法控制函数的代码,除了运行你自己的无限循环之外,没有办法阻止它再次继续运行(当然你不会想这么做)。

英文:

> what's the simplest way to solve this?

You can't, within the same execution environment. If you don't control what's inside the function, there's no way for you to stop it from your code. Once you've made the call to someFunction, your code doesn't get control back until that function has run to completion.¹ If it loops infinitely, it loops infinitely.

You could spawn a worker thread or child process that runs the function in a separate environment, which you can then terminate (1, 2) after a period of time. But within your main environment, you can't do it.


¹ Note that in the case of an async function, "run(s) to completion" means "reaches the point where it returns its promise" (the first await, return [implicit or explicit], or uncaught error). So in theory if you had an async function that started something then didn't go into the finite loop until after the first await (say), your code would get an opportunity to run, but if you have no control over the function's code there's nothing you can do from keeping it from continuing again (other than run an infinite loop of your own, which of course you wouldn't want to do).

huangapple
  • 本文由 发表于 2023年1月5日 18:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75017366.html
匿名

发表评论

匿名网友

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

确定