What is the difference between forking and threadi

2019-06-27 21:29发布

问题:

Reading the documentation for the spawn gem it states:

By default, spawn will use the fork to spawn child processes. You can configure it to do threading either by telling the spawn method when you call it or by configuring your environment. For example, this is how you can tell spawn to use threading on the call,

What would be the difference between using a fork or a thread, what are the repercussions of either decision, and how do I know which to use?

回答1:

Threading means you run the code in another thread in the same process whereas forking means you fork a separate process.

Threading in general means that you'll use less memory since you won't have a separate application instance (this advantage is lessened if you have a copy on write friendly ruby such as ree). Communication between threads is also a little easier.

Depending on your ruby interpreter, ruby may not use extra cores efficiently (jruby is good at this, MRI much worse) so spawning a bunch of extra threads will impact the performance of your web app and won't make full use of your resources - MRI only runs one thread at a time

Forking creates separate ruby instances so you'll make better use of multiple cores. You're also less likely to adversely affect your main application. You need to be a tiny bit careful when forking as you share open file descriptors when you fork, so you usually want to reopen database connections, memcache connections etc.

With MRI I'd use forking, with jruby there's more of a case to be made for threading



回答2:

Fork creates another process and processes are generally designed to run independently of whatever else is going on in your application. Processes do not share resources.

Threads, however, are designed for a different purpose. You would want to use a thread if you wish to parallelize a certain task.

"A fork() induces a parent-child relationship between two processes. Thread creation induces a peer relationship between all the threads of a process."

Read a more extensive explanation on this link.