Go Concurrency Model Learning

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

Go Concurrency Model: original address:

Http://www.oschina.net/translate/go-concurrency-patterns-pipelines?print

Characteristics: Atomic concurrency characteristics

Advantage: TheGolang can effectively use I/O and multi-CPU features.

1 What is a pipe

There is no clear definition of pipelines in Golang; it is just one of many concurrent programs. A pipeline is a series of stages of a channel connection, each of which runs the same function as a set of goroutine. At each stage, the Goroutine run steps are:

    • Accepted value from upstream via inbound channel

    • Perform some functional operations on the data, usually resulting in new values

    • through the downstream Environment Channel Send value

In addition to the initial and final stages there is only one entry point or one exit route, and there are any number of entry and exit routes at each stage. The starting stage is sometimes referred to as the source or producer, and the last stage is called sink or consumer.


As an example:

Phase one: Gen, which converts an integer list to a channel in a way that reads integers from a list. After the Gen function begins Goroutine, an integer is sent on the channel and the channel is closed after all values have been sent:

Func Gen (nums ... int) <-chan int {out    : = make (chan int)    go func () {        to _, N: = Range Nums {out            <- N        }        Close (out)    } ()    return out}

Second Stage: Sq, which takes an integer from the channel, and then returns the square of each integer value that is received to a channel. Close the exit channel after the end of the entry channel closing and sending all downstream values:

Func sq (in <-chan int) <-chan int {out    : = Do (chan int)    go func () {for        N: = Range ' {out            <- n * N       }        Close (out) on    } ()    return out}
The main function establishes the channel and runs the last stage: it accepts values from the second stage and prints out each value until the channel is closed:
Func Main () {    //Set up the pipeline.    c: = Gen (2, 3) out    : = sq (c)     //consume the output.    Fmt. PRINTLN (<-out)//4    FMT. PRINTLN (<-out)//9

fan out, fan in

Fan-Out (fan-out): Multiple functions can read data from the same channel until the channel is closed; This provides a way to distribute tasks in a set of "people", allowing for parallel processing of CPU and I/O.

Fan-In (fan-in): A function can read and process data from multiple inputs, and the multiple input channels are mapped to a single channel, which is closed as all inputs end.

Short Stop

Pipeline function Model:

    • When all the send operations are finished, the stage closes their exit lanes.

    • the period of continuous receipt from the entry channel values until those channels are closed.


This model allows each receiving phase to write data through a range loop, ensuring that once all the values sent downstream are successfully sent, all goroutine exits.

But on a real pipeline, the stage does not always receive all the entry values. Sometimes the design is like this: the receiver may only need a subset value to make progress. More often than not, an early exit is a stage, because an entry value represents an earlier stage of error. In both cases, the receiver should not wait for the remaining value to arrive, and we want the early stage to stop producing values that are not needed in the subsequent stages.


Explicit cancellation

When main decides to exit when it does not accept all the values of out, it must inform the upstream state (upstream stage) of the goroutines and let it discard the data that is being sent. It can be done by sending data on a channel called done. There are two blocked senders in the example, so there are two groups of values sent:

The following is a list of guidelines for building pipelines:

    • The status will close their outgoing channel after all the send operations have been completed

    • The status continues to receive values entered from the incoming channel until the channel is closed or its sender is released.

The pipeline is either guaranteed enough to store all the buffers that send the data, or to receive a signal from the receiver that the channel should be discarded to ensure that the sender is released.


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.