[英]What happens if I don't $redis = Redis.new in after_fork?
What's the negative impact of not putting $redis = Redis.new
in Unicorn's after_fork, since redis-rb is thread safe? 由于redis-rb是线程安全的,因此不将
$redis = Redis.new
放入Unicorn的after_fork有什么负面影响? Assuming I have more than one worker. 假设我有一个以上的工人。
As opposed to just putting that line of code in the environment.rb or an intializer? 与仅将那行代码放入environment.rb或初始化器相反?
after_fork
has little to do with thread safety. after_fork
与线程安全性无关。 It it used when a parent process forks a child process, not when spawning a thread. 它在父进程派生子进程时使用,而不是在生成线程时使用。
Why you should care 为什么你应该关心
If you never fork, you probably don't. 如果您从不分叉,您可能不会。
When you fork, the parent and child processes share file and socket descriptors (db connections, redis connections). 进行分叉时,父进程和子进程共享文件和套接字描述符(db连接,redis连接)。 If you don't re-open the connection to Redis in the child, data from the parent can be interleaved on the socket with data from the child.
如果不重新打开子级中与Redis的连接,则可以将父级中的数据与子级中的数据交织在套接字上。
Read A Unix Shell in Ruby Pipes for a much more detailed description of what's going on. 阅读Ruby Pipes中的Unix Shell,了解发生了什么的详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.