Fanout-Easier to write concurrency code

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

There is no need to manage the grouting and channel. The following is a forward golangtc.com original:

Just wrote a package that felt worth sharing under:

Document: Https://github.com/sunfmin/fanout

To simplify the writing of concurrent programs (Goroutine, channel), this package directly overwrites the last sample program from the Go Concurrency patterns:pipelines blog.

Usually write with goroutine and channel procedures, always explode "fatal Error:all Goroutines is asleep-deadlock!", check up and difficult to find reasons.

Example program-run WHOIS with 60 goroutine to see if the domain name is still in:

inputs := []interface{}{}for _, word:= range domainWords {    inputs = append(inputs, word)}results, err2 := fanout.ParallelRun(60, func(input interface{})(interface{}, error) {    word := input.(string)    if strings.TrimSpace(word) == "" {        return nil, nil    }    py := pinyin.Convert(word)    pydowncase := strings.ToLower(py)    domain := pydowncase + ".com"    outr, err := domainAvailable(word, domain)    if outr.available {        fmt.Printf("[Ohh Yeah] %s %s\n", outr.word, outr.domain)    } else {        fmt.Printf("\t\t\t %s %s %s\n", outr.word, outr.domain, outr.summary)    }    if err != nil {        fmt.Println("Error: ", err)    }    return outr, nil}, inputs)fmt.Println("Finished ", len(results), ", Error:", err2)

A diagram to illustrate:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.