我有一个进程,它使用concurrent-ruby gem来同时处理大量的API调用Concurrent::Future.execute
,并且在一段时间后它会死掉:
ERROR -- : can't create Thread (11) (ThreadError)
/current/vendor/bundler_gems/ruby/2.0.0/bundler/gems/concurrent-ruby-cba3702c4e1e/lib/concurrent/executor/ruby_thread_pool_executor.rb:280:in `initialize'
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以告诉Concurrent
它限制它产生的线程数,因为我无法预先知道它需要进行多少次API调用?
或者这是我需要在我的应用程序中明确编码的东西?
我正在使用Ruby 2.0.0
(唉目前没有选择改变它)
我有一个方法迭代一些记录并对它们做一些事情,可选择产生一个结果 - 类似于:
@evens = []
def foo(numbers)
numbers.each do |r|
even = r % 2 == 0
@evens << r if even
yield "#{r}: #{even}" if block_given?
end
end
Run Code Online (Sandbox Code Playgroud)
我有第二个方法来获取记录并将它们传递给第一个方法,类似于:
def bar(count)
numbers = (0..count).to_a
foo(numbers)
end
Run Code Online (Sandbox Code Playgroud)
我想更改此方法以获取可选块,从而产生第一种方法的结果.到目前为止我想出的是:
def bar(count)
numbers = (0..count).to_a
foo(numbers) do |result|
yield result if block_given?
end
end
Run Code Online (Sandbox Code Playgroud)
这感觉就像if block_given?
我应该需要的两条线和一条支票.有没有更简洁的方式简单地说"产生任何这种方法调用产生,如果有的话"?
这是一个Java问题.
将a转换List<?>
为a 的最快方法是List<ObjectType>
什么?我知道这可以通过迭代,请排除该选项.
迭代的例子,
final List<ObjectType> targetList = new ArrayList<ObjectType>();
// API returns List<?> so I have no choice.
List<?> resultList = resultSet.getResults();
// This is slow. Average list size is 500,000 elements.
while (resultList != null && !resultList.isEmpty()) {
for (final Object object : resultList) {
final ObjectType objectType = (ObjectType) object;
targetList.add(objectType);
}
resultSet = someService.getNext(resultSet);
resultList = resultSet.getList();
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
def doubleList(noList:List[Int]) = {
val result = noList.map{ number =>
number*2
}
result
}
def halfList(noList:List[Int]) = {
val result = noList.map{ number =>
number/2
}
result
}
def mapFunctionDRY(noList:List[Int])(codeBlock: () => Int) = {
}
println(halfList(List(1,2,3)))
println(doubleList(List(1,2,4)))
Run Code Online (Sandbox Code Playgroud)
我玩弄Scala和发现违反干(不要重复自己)在上述两种功能doubleList
和halfList
.我希望函数中的公共代码被隔离,只需传递不同的代码块.这样我的代码就不会违反DRY原则.我知道你可以在scala中传入代码块作为参数.这就是我打算做的事情mapFunctionDRY
我希望mapFunctionDRY以这种方式
def mapFunctionDRY(noList:List[Int])(codeBlock: () => Int) = {
noList.map{ number =>
codeBlock()
}
}
Run Code Online (Sandbox Code Playgroud)
和代码doubleList
,并halfList
以与此类似
def doubleList(noList:List[Int]) = { mapFunctionDRY(noList){ () => number*2 } }
Run Code Online (Sandbox Code Playgroud)
但如果我这样做,我会得到一个编译错误.在这种情况下,如何将代码作为参数传入,以避免违反DRY.可以进一步减少此代码以使其干燥吗?