2013-08-16 4 views
21

का उपयोग कैसे करें मैं याहू वित्त से स्टॉक मूल्य स्प्रैडशीट्स डाउनलोड करने के लिए गो का उपयोग करना चाहता हूं। मैं अपने स्टॉक में अपने स्टॉक के लिए एक http अनुरोध करूँगा। मेरे पास लगभग 2500 प्रतीकों की एक सूची है, लेकिन समानांतर में 2500 अनुरोध करने की बजाय, मैं एक समय में 250 बनाना पसंद करूंगा। जावा में मैं एक थ्रेड पूल बनाउंगा और जब वे मुफ्त में हों तो थ्रेड का पुन: उपयोग करें। मैं कुछ ऐसा खोजने की कोशिश कर रहा था, एक गोरौटाइन पूल, यदि आप करेंगे, लेकिन कोई संसाधन नहीं ढूंढ पाए। मैं सराहना करता हूं कि कोई मुझे बता सकता है कि कार्य को कैसे पूरा किया जाए या मुझे संसाधनों के लिए इंगित करें। धन्यवाद!गोरौटाइन पूल

+0

क्या आपको पूल में इन goroutines की आवश्यकता है? जैसा कि, आप उन संसाधनों की तरह व्यवहार करते हैं जिन्हें आप बनाते हैं और पुन: उपयोग करते हैं। या, क्या आप एक सरल समाधान पर विचार करेंगे जहां गोरोटाइन डिस्पोजेबल हैं, लेकिन आप बस नियंत्रित करते हैं कि उनमें से कितने समय में चल रहे हैं? – atedja

उत्तर

41

मुझे लगता है कि 250 goroutines बनाने का सबसे आसान तरीका है और उन्हें एक चैनल पास करना है जिसका उपयोग आप मुख्य गोरौटाइन से लेकर बच्चों को लिंक पास करने के लिए कर सकते हैं, उस चैनल को सुन सकते हैं।

सभी लिंक goroutines को पास किया जाता है, तो आप एक चैनल को बंद करने और सभी goroutines सिर्फ अपनी नौकरी खत्म।

मुख्य goroutine से अपने आप को सुरक्षित करने के लिए इससे पहले कि बच्चों प्रक्रिया डेटा समाप्त हो गया हो, आप sync.WaitGroup उपयोग कर सकते हैं।

यहाँ कुछ वर्णन करने के लिए कोड है (नहीं एक अंतिम कार्यरत वर्शन लेकिन बात से पता चलता) कि मुझे बताया गया है इसके बाद के संस्करण:

func worker(linkChan chan string, wg *sync.WaitGroup) { 
    // Decreasing internal counter for wait-group as soon as goroutine finishes 
    defer wg.Done() 

    for url := range linkChan { 
    // Analyze value and do the job here 
    } 
} 

func main() { 
    lCh := make(chan string) 
    wg := new(sync.WaitGroup) 

    // Adding routines to workgroup and running then 
    for i := 0; i < 250; i++ { 
     wg.Add(1) 
     go worker(lCh, wg) 
    } 

    // Processing all links by spreading them to `free` goroutines 
    for _, link := range yourLinksSlice { 
     lCh <- link 
    } 

    // Closing channel (waiting in goroutines won't continue any more) 
    close(lCh) 

    // Waiting for all goroutines to finish (otherwise they die as main routine dies) 
    wg.Wait() 
} 
+3

यहां इस कोड का एक छोटा सा परीक्षण कार्रवाई में है: http://play.golang.org/p/fruJiGBWjn – Druska

1

आप इस git repo

से Go में पूल धागा कार्यान्वयन पुस्तकालय का उपयोग कर सकते Here कैसे ब्लॉग से

स्निपेट थ्रेड पूल के रूप में चैनल का उपयोग करने के बारे में अच्छा ब्लॉग है

var (
MaxWorker = os.Getenv("MAX_WORKERS") 
MaxQueue = os.Getenv("MAX_QUEUE") 
) 

//Job represents the job to be run 
type Job struct { 
    Payload Payload 
} 

// A buffered channel that we can send work requests on. 
var JobQueue chan Job 

// Worker represents the worker that executes the job 
type Worker struct { 
    WorkerPool chan chan Job 
    JobChannel chan Job 
    quit  chan bool 
} 

func NewWorker(workerPool chan chan Job) Worker { 
    return Worker{ 
     WorkerPool: workerPool, 
     JobChannel: make(chan Job), 
     quit:  make(chan bool)} 
} 

// Start method starts the run loop for the worker, listening for a quit channel in 
// case we need to stop it 
func (w Worker) Start() { 
    go func() { 
     for { 
      // register the current worker into the worker queue. 
      w.WorkerPool <- w.JobChannel 

      select { 
      case job := <-w.JobChannel: 
       // we have received a work request. 
       if err := job.Payload.UploadToS3(); err != nil { 
        log.Errorf("Error uploading to S3: %s", err.Error()) 
       } 

      case <-w.quit: 
       // we have received a signal to stop 
       return 
      } 
     } 
    }() 
} 

// Stop signals the worker to stop listening for work requests. 
func (w Worker) Stop() { 
    go func() { 
     w.quit <- true 
    }() 
} 
संबंधित मुद्दे