2012-09-25 8 views
16

मैंने हाल ही में प्ले फ्रेमवर्क 2.0 में स्थानांतरित कर दिया है और मेरे बारे में कुछ प्रश्न हैं जो नियंत्रक वास्तव में खेल में काम करते हैं।प्ले फ्रेमवर्क 2.0 नियंत्रक/Async कैसे काम करता है?

play docs में उल्लेख कर रहे हैं:

जिस तरह से

प्ले 2.0 काम करता है, कार्रवाई कोड जितनी जल्दी संभव (यानी गैर अवरुद्ध।) होना चाहिए।

another part of the docs में

हालांकि:

  /actions { 
       router = round-robin 
       nr-of-instances = 24 
      } 

और

 actions-dispatcher = { 
      fork-join-executor { 
       parallelism-factor = 1.0 
       parallelism-max = 24 
      } 
     } 

ऐसा लगता है 24 अभिनेताओं से निपटने नियंत्रकों के लिए आवंटित देखते हैं कि। मुझे लगता है कि प्रत्येक अनुरोध अनुरोध के जीवनकाल के लिए उन कलाकारों में से एक आवंटित करता है। क्या यह सही है?

इसके अलावा, parallelism-factor का अर्थ क्या है और fork-join-executorthread-pool से भिन्न कैसे है?

इसके अलावा - दस्तावेज़ों को यह कहना चाहिए कि लंबे समय तक गणना के लिए Async का उपयोग किया जाना चाहिए। लंबी गणना के रूप में क्या योग्यता प्राप्त करता है? 100ms? 300 मि.से? 5 सेकंड? दस पल? मेरा अनुमान एक सेकंड में कुछ भी होगा, लेकिन यह कैसे निर्धारित करें?

इस प्रश्न का कारण यह है कि परीक्षण एसिंक नियंत्रक कॉल नियमित कॉल से कठिन है। आपको नकली एप्लिकेशन को स्पिन करना होगा और केवल एक विधि को कॉल करने और इसके वापसी मूल्य की जांच करने के बजाय पूर्ण अनुरोध करना होगा।

भले ही यह मामला न हो, मुझे संदेह है कि Async और Akka.future में सब कुछ लपेटना तरीका है।

मैंने इसे #playframework आईआरसी चैनल में पूछा है लेकिन इसका कोई जवाब नहीं था और ऐसा लगता है कि मैं अकेला नहीं हूं कि चीजें कैसे की जानी चाहिए।

बस दोहराते हैं:

  1. सही है कि हर अनुरोध से/कार्यों पूल एक अभिनेता आवंटित यह है?
  2. parallelism-factor का अर्थ क्या है और यह 1 क्यों है?
  3. fork-join-executorthread-pool-executor से भिन्न कैसे है?
  4. Async में गणना कब तक की जानी चाहिए?
  5. फर्जी अनुप्रयोगों को कताई किए बिना एसिंक नियंत्रक विधि का परीक्षण करना संभव नहीं है?

अग्रिम धन्यवाद।

संपादित करें: आईआरसी से

आईआरसी से कुछ सामान कुछ सामान।

<imeredith> arturaz: i cant be boethered writing up a full reply but here are key points 
<imeredith> arturaz: i believe that some type of CPS goes on with async stuff which frees up request threads 
<arturaz> CPS? 
<imeredith> continuations 
<imeredith> when the future is finished, or timedout, it then resumes the request 
<imeredith> and returns data 
<imeredith> arturaz: as for testing, you can do .await on the future and it will block until the data is ready 
<imeredith> (i believe) 
<imeredith> arturaz: as for "long" and parallelism - the longer you hold a request thread, the more parrellism you need 
<imeredith> arturaz: ie servlets typically need a lot of threads because you have to hold the request thread open for a longer time then if you are using play async 
<imeredith> "Is it right that every request allocates one actor from /actions pool?" - yes i belive so 
<imeredith> "What does parallelism-factor mean and why is it 1?" - im guessing this is how many actors there are in the pool? 
<imeredith> or not 
<imeredith> "How does fork-join-executor differ from thread-pool-executor?" -no idea 
<imeredith> "How long should a calculation be to become wrapped in Async?" - i think that is the same as asking "how long is a piece of string" 
<imeredith> "Is is not possible to test async controller method without spinning up fake applications?" i think you should be able to get the result 
<viktorklang> imeredith: A good idea is to read the documentation: http://doc.akka.io/docs/akka/2.0.3/general/configuration.html (which says parallelism-factor is: # Parallelism (threads) ... ceil(available processors * factor)) 
<arturaz> viktorklang, don't get me wrong, but that's the problem - this is not documentation, it's a reminder to yourself. 
<arturaz> I have absolutely no idea what that should mean 
<viktorklang> arturaz: It's the number of processors available multiplied with the factor you give, and then rounded up using "ceil". I don't know how it could be more clear. 
<arturaz> viktorklang, how about: This factor is used in calculation `ceil(number of processors * factor)` which describes how big is a thread pool given for your actors. 
<viktorklang> arturaz: But that is not strictly true since the size is also guarded by your min and max values 
<arturaz> then why is it there? :) 
<viktorklang> arturaz: Parallelism (threads) ... ceil(available processors * factor) could be expanded by adding a big of conversational fluff: Parallelism (in other words: number of threads), it is calculated using the given factor as: ceil(available processors * factor) 
<viktorklang> arturaz: Because your program might not work with a parallelism less than X and you don't want to use more threads than X (i.e if you have a 48 core box and you have 4.0 as factor that'll be a crapload of threads) 
<viktorklang> arturaz: I.e. scheduling overhead gives diminishing returns, especially if ctz switching is across physical slots. 
<viktorklang> arturaz: Changing thread pool sizes will always require you to have at least basic understanding on Threads and thread scheduling 
<viktorklang> arturaz: makes sense? 
<arturaz> yes 
<arturaz> and thank you 
<arturaz> I'll add this to my question, but this kind of knowledge would be awesome docs ;) 

उत्तर

6
  1. संदेश एक अभिनेता के एक अभिनेता पर आता है तो उस पर कि अभिनेता को जब तक यह है कि संदेश के संसाधन में लगने रखती है। यदि आप अनुरोध को सिंक्रनाइज़ करते हैं (उस संदेश की प्रसंस्करण के दौरान पूरी प्रतिक्रिया की गणना करें), तो यह अभिनेता प्रतिक्रिया के पूरा होने तक अन्य अनुरोधों की सेवा नहीं कर सकता है। यदि आप इस अनुरोध की प्राप्ति के बाद, किसी अन्य अभिनेता को काम भेज सकते हैं, तो जिस अभिनेता को अनुरोध प्राप्त हुआ वह अगले अनुरोध पर काम करना शुरू कर सकता है जबकि अन्य कलाकारों द्वारा पहला अनुरोध किया जा रहा है।

  2. अभिनेताओं के लिए इस्तेमाल किया धागे की संख्या है "संख्या CPUs * समानांतरवाद कारक" (आप हालांकि मिनट निर्दिष्ट और अधिकतम कर सकते हैं)

  3. पता नहीं

  4. जब तक वास्तविक चल रहा गणना कर रहे हैं, मैं किसी अन्य सिस्टम से बात कर रहे एसिंक को कुछ भी करने की इच्छा है, जैसे डेटाबेस/फाइल सिस्टम के साथ आईओ करना। निश्चित रूप से कुछ भी जो थ्रेड को अवरुद्ध कर सकता है। हालांकि, चूंकि संदेशों को पारित करने में बहुत कम ओवरहेड है, लेकिन मुझे नहीं लगता कि सभी कलाकारों को सिर्फ सभी काम भेजने के साथ कोई समस्या होगी।

  5. अपने नियंत्रकों का परीक्षण करने के तरीके के बारे में Play Documentation on functional tests देखें।

+0

बिंदु 1. क्या कार्यों के बजाय सिर्फ अन्य अभिनेताओं में बंद भेजने के लिए धागे की संख्या में वृद्धि के लाभ के बारे में है/कार्यों 150 पसंद करने के लिए (150 समवर्ती कार्यों के लिए)? – arturaz

+0

इस तरह से सोचें। आपके डेस्क पर करने के लिए आपके पास सैकड़ों चीजें हैं। कौन सा अधिक कुशल होगा? ढेर से एक खींचो, जब तक यह पूरा नहीं हो जाता तब तक काम करें, फिर अगले पर काम करें। या उनमें से पहले 150 ले लो और 150 अलग-अलग चीजों के बीच अपना समय बांटने पर प्रत्येक पर एक छोटा सा काम करें। पहला अधिक कुशल है क्योंकि आप "संदर्भ स्विच" पर समय बर्बाद नहीं करते हैं। वही सच है। – stew

+0

लेकिन किसी अन्य अभिनेता को कार्य भेजना एक संदर्भ स्विच की ओर जाता है। क्या फायदा है –

1

ऐसा लगता है आप परीक्षण के लिए ऐसा कर सकते हैं कि:

object ControllerHelpers { 
    class ResultExtensions(result: Result) { 
    /** 
    * Retrieve Promise[Result] from AsyncResult 
    * @return 
    */ 
    def asyncResult = result match { 
     case async: AsyncResult => async.result 
     case _ => throw new IllegalArgumentException(
     "%s of type %s is not AsyncResult!".format(result, result.getClass) 
    ) 
    } 

    /** 
    * Block until result is available. 
    * 
    * @return 
    */ 
    def await = asyncResult.await 

    /** 
    * Block until result is available. 
    * 
    * @param timeout 
    * @return 
    */ 
    def await(timeout: Long) = asyncResult.await(timeout) 

    /** 
    * Block for max 5 seconds to retrieve result. 
    * @return 
    */ 
    def get = await.get 
    } 
} 

    implicit def extendResult(result: Result) = 
    new ControllerHelpers.ResultExtensions(result) 


    val result = c.postcodeTimesCsv()(request(params)).get 
    status(result) should be === OK 
संबंधित मुद्दे