在后台进程中进行分叉和线程处理有什么区别? [英] What is the difference between forking and threading in a background process?

查看:72
本文介绍了在后台进程中进行分叉和线程处理有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读其生成的宝石的文档:

Reading the documentation for the spawn gem it states:

默认情况下,spawn将使用fork生成子进程.你可以 通过告诉spawn方法何时将其配置为执行线程 您可以调用它,也可以通过配置环境来调用它.例如,这是 如何告诉Spawn在调用中使用线程,

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?

推荐答案

线程化意味着您在同一进程中的另一个线程中运行代码,而分叉意味着您分叉了一个单独的进程.

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

一般来说,线程化意味着您将使用更少的内存,因为您将没有单独的应用程序实例(如果您在ree等可写红宝石上复制了副本,则此优势会减弱).线程之间的通信也更容易一些.

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.

取决于您的ruby解释器,ruby可能无法有效地使用额外的内核(jruby擅长于此,MRI则更糟),因此产生大量额外线程将影响您的Web应用程序的性能,并且不会充分利用您的资源-MRI一次只能运行一个线程

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

分叉会创建单独的ruby实例,因此您将更好地利用多个内核.您也不太可能对您的主应用程序产生不利影响.进行分叉时,您需要格外小心,因为在分叉时共享打开的文件描述符,因此通常需要重新打开数据库连接,内存缓存连接等.

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.

使用MRI时,我会使用分叉,而使用jruby时,会有更多情况下需要穿线

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

这篇关于在后台进程中进行分叉和线程处理有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆