NSQ inside waitgroups Two kinds of practical usage

Source: Internet
Author: User

I have seen it several times, I think it is very practical, keep a record.

1. After opening a lot of goroutine, wait for execution to complete.

struct {    *waitgroupwrapper) Wrap (CB func ()) {    W.add (1)    go func () {        CB ()        w.done ()    } ()}//  can be used as follows:WG: = WAITGROUPWRAPPER{}WG. Wrap (func () {n.idpump ()}) ... WG. Wait ()

2. Program to close, notify each goroutine safe exit

func work () {Exitchan:= Make (chanint) Go Task1 (Exitchan) go Task2 (Exitchan) time. Sleep (5*Time . Second) Close (Exitchan)}func Task1 (Exitchan Chanint) {    <-Exitchan Log. Printf ("Task1 exiting")}func Task2 (Exitchan Chanint) {    <-Exitchan Log. Printf ("Task2 exiting")}

Original reference:

Http://nsq.io/overview/internals.html

NSQ inside waitgroups Two kinds of practical usage

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.