Ruby 1.8 线程在 Phusion Passenger 中的行为如何?
如果我在控制器操作中触发其中 1 个或 1000 个:
Thread.new {
# do some stuff
}
- 它们确实会与 http 请求异步运行吗?
- 如果引发异常,它会流到哪里?
- 还有什么我应该知道的吗?
If I fire of 1 or 1000 of these in a controller action:
Thread.new {
# do some stuff
}
- Will they indeed run asynchronously with the http request?
- If an exception is raised, where does it trickle up to?
- Anything else I should know about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
线程和异常不一定是朋友,因为异常无法脱离当前线程并警告父线程。您还需要打开线程异常通知,否则您根本不会听到它们:
您还需要在每个新线程上调用 Thread.join ,否则主线程将在没有它们的情况下匆忙运行。
这样,您的代码至少会在出现异常时停止,而不是简单地终止生成异常的线程并继续运行,就好像什么也没发生一样。
确保您在线程内调用的内容是线程安全的,否则您可能会得到意外的结果。
Threads and exceptions are not necessarily friends since exceptions can't bust out of the current thread and alert the parent thread. You also need to turn on thread exception notification or you'll never hear about them at all:
You'll also need to call
Thread.join
on each new thread or the main one will hurry along without them.This way your code will at least halt on an exception instead of simply terminating the thread that generated one and continuing on as if nothing had happened.
Make sure that the things you're calling inside your thread are thread safe or you may get unexpected results.