Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中的线程。线程池的替代方案_Ruby_Multithreading - Fatal编程技术网

Ruby中的线程。线程池的替代方案

Ruby中的线程。线程池的替代方案,ruby,multithreading,Ruby,Multithreading,假设我有两个不同的表格需要填写。每个表可以有不同数量的源文件。我想并行地填充这些表,但是每个源文件都将被读取。 示例:表A:3源文件,表B:1源文件 tables = {'A': [sourcefileA_1, sourcefileA_2,sourcefileA_3], 'B':[sourcefileB_1]} threads = [] tables.each do |table,source_files| threads << Thread.new { do_something(

假设我有两个不同的表格需要填写。每个表可以有不同数量的源文件。我想并行地填充这些表,但是每个源文件都将被读取。 示例:表A:3源文件,表B:1源文件

tables = {'A': [sourcefileA_1, sourcefileA_2,sourcefileA_3], 'B':[sourcefileB_1]}
threads = []
tables.each do |table,source_files|
 threads << Thread.new { do_something(table, source_files)}
end
threads.each(&:join)
def do_something(table, source_files)
 source_files.each do |s_file|
 #do something
end
未指定b,但它不会显示正常错误消息。

首先

查看有关错误处理的(答案)

第二,有一个很好的gem,叫做ruby,它在其他语言中实现了多线程的许多方面


另一个要考虑的问题是,红宝石不是真正的并发,因为。但你有光纤,这对你的情况很好,异步IO。

问题标题太宽泛了;但是,从Thread.pool获取错误的问题非常突出。也许您应该更新标题以使其更具体。关于错误处理的答案是:如果您不将此变量设置为true,则只有在您调用线程的“连接”或“线程”值时才会引发异常。我加入了线程,所以我捕捉到了错误。答案还考虑了
thread
的用法,在我的例子中
thread.pool(4)
不会抛出异常。所以,这个答案对我没有帮助。关于吉尔,我不确定我是否正确理解你。我做了一个有线程和无线程的基准测试。使用线程的速度要快得多。为什么我会得到这样的基准测试结果?关于基准测试,当您查询外部服务(db和文件)时,可能会有一个上下文切换,直到io返回。您能详细说明一下吗?上下文切换可能导致更长的处理时间,但根据您的消息,它确实加快了处理速度。因此,基本上我现在要做的是,我没有并行运行表,对吗(不是在不同的处理器上)?
require 'thread/pool' # from the ruby-thread gem
pool = Thread.pool(4)
pool.process do 
 puts(b)
end