Using Parallel gem in Ruby; how many cores to use?

时间:2016-04-12 00:54:56

标签: ruby multithreading concurrency parallel-processing

I have a section of slow code that I want to speed up by using the parallel gem and multiple cores.

With the Parallel gem, you must specify the number of processes or threads you want to use, so I hard-coded it to use the same number of logical cores that I have. It works perfectly, but my problem is that this code is intended to be distributed and used by other people who may have a different number of cores.

Should I try to detect the number of cores that their machine has, and use that number? Or should I default to no parallelism and only switch to multi-threaded code if the user explicitly specifies the number of threads they'd like to use? (e.g. pg_restore)

If I do try to detect cores, should I try to utilise all cores found, or would it be more polite to use, say, all but one of the cores?

1 个答案:

答案 0 :(得分:2)

不知道您的程序是如何占用内存密集型的,但这些要求也可能会给内存较少的人造成重大意外问题,而不是您正在测试它的计算机。

由于它是一个CLI工具,为什么不添加一个像--procs这样的标志来获取要使用的进程数量的参数,并由用户决定?