वायदा के लिए स्काला का धागा पूल कितना बड़ा है?
मेरा स्काला एप्लिकेशन कई लाख बनाता है future {}
और मुझे आश्चर्य है कि अगर कोई चीज है तो मैं थ्रेड पूल को कॉन्फ़िगर करके उन्हें ऑप्टिमाइज़ कर सकता हूं।
धन्यवाद।
वायदा के लिए स्काला का धागा पूल कितना बड़ा है?
मेरा स्काला एप्लिकेशन कई लाख बनाता है future {}
और मुझे आश्चर्य है कि अगर कोई चीज है तो मैं थ्रेड पूल को कॉन्फ़िगर करके उन्हें ऑप्टिमाइज़ कर सकता हूं।
धन्यवाद।
The reason is that map, flatMap methods of Action allows you to call arbitrary code when joining the actions together. Slick cannot allow that code to be run on its own execution context, because it has no way to know if you are going to tie up Slicks threads for a long time.
जवाबों:
आप अपने खुद के ExecutionContext को निर्दिष्ट कर सकते हैं कि आपका निहितार्थ वैश्विक निहित ExecutionContext को आयात करने के बजाय में चलेगा।
import java.util.concurrent.Executors
import scala.concurrent._
implicit val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(1000)
def execute(runnable: Runnable) {
threadPool.submit(runnable)
}
def reportFailure(t: Throwable) {}
}
यह उत्तर भिक्षुक से है, स्वीकृत उत्तर से एक टिप्पणी है। हालांकि, कोई भी इस महान जवाब को याद कर सकता है इसलिए मैं इसे यहां दोहरा रहा हूं।
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
यदि आपको केवल थ्रेड पूल काउंट को बदलने की आवश्यकता है, तो बस वैश्विक निष्पादक का उपयोग करें और निम्नलिखित सिस्टम गुण पास करें।
-Dscala.concurrent.context.numThreads=8 -Dscala.concurrent.context.maxThreads=8
स्कैल फ्यूचर में थ्रेडपूल निर्दिष्ट करने का सबसे अच्छा तरीका:
implicit val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(conf.getInt("5"));
override def reportFailure(cause: Throwable): Unit = {};
override def execute(runnable: Runnable): Unit = threadPool.submit(runnable);
def shutdown() = threadPool.shutdown();
}
class ThreadPoolExecutionContext(val executionContext: ExecutionContext)
object ThreadPoolExecutionContext {
val executionContextProvider: ThreadPoolExecutionContext = {
try {
val executionContextExecutor: ExecutionContextExecutor = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(25))
new ThreadPoolExecutionContext(executionContextExecutor)
} catch {
case exception: Exception => {
Log.error("Failed to create thread pool", exception)
throw exception
}
}
}
}