在这种情况下是否应该使用多个线程? [红宝石] [英] Should I use multiple threads in this situation? [Ruby]

查看:107
本文介绍了在这种情况下是否应该使用多个线程? [红宝石]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一次打开多个文件并处理它们.这些文件包含用于分隔数据的令牌,因此有时对一个文件的处理可能必须等待其他文件才能赶上相同的令牌.

I'm opening multiple files and processing them, one line at a time. The files contain tokens separating the data, such that sometimes the processing of one file may have to wait for others to catch up to that same token.

最初,我只使用一个线程和一个数组来执行此操作,该数组用true/false指示是应在当前迭代中读取文件还是应等待其他一些赶上来.

I was doing this initially with only one thread and an array indicating with true/false if the file should be read in the current iteration or if it should wait for some of the others to catch up.

使用线程会更简单吗?更高效? Ruby对此有一种机制吗?

Would using threads make this simpler? More efficient? Does Ruby have a mechanism for this?

推荐答案

我不确定在ruby中使用线程是否有益.最近,我编写并测试了一个应该进行并行计算的应用程序,但是即使在四核处理器上,我也没有得到我期望的结果,它按顺序执行计算,一个线程接一个线程.阅读文章,其中有关于线程调度的讨论,可能会发现,至少对于原始红宝石而言,情况没有改变.

I'm not sure if using threads in ruby is beneficial. Recently I've written and tested an application which was supposed to do parallel computations, but I didn't get what I expected even on quad core processor, it performed computations sequentially, one thread after another. Read this article, it has discussion about threads scheduling, it may turn out that things haven't changed at least for original ruby.

这篇关于在这种情况下是否应该使用多个线程? [红宝石]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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