Back

ruby - 多线程编程,可以不用使用join

发布时间: 2019-08-26 10:08:00

参考:https://stackoverflow.com/questions/7905808/is-it-ok-not-to-call-threadjoin

join的目的是为了让当前主thread等待子thread,然后返回对应的结果.

一旦多线程用熟悉了的话,是不需要join的.

不过需要注意一点: 

一旦主进程结束, 子进程就跟着结束(起码在ruby中是这样)

其他语言也是一样的.

C:   https://stackoverflow.com/questions/30370702/c-child-threads-terminating-on-main-parent-thread-exit

https://stackoverflow.com/questions/11875956/when-the-main-thread-exits-do-other-threads-also-exit

但是Java 的就不一样

https://stackoverflow.com/questions/22163922/how-can-child-threads-still-execute-even-after-that-their-parent-thread-die-or-t

根据我在2011年MOTO项目的经验, 这个也跟webserver相关. 

thin, mongrel 这样的"固定进程"的服务器,就可以.

passenger, (可能包括puma , unicorn 等)会"spawn" 新进程的,估计就不行.

所以,最安全的还是跑side kiq, delayed job

Back