Practical experience of concurrent programming in Golang: from Goroutines to large-scale clusters

王林
Release: 2023-07-18 09:05:31
Original
1136 people have browsed it

Practical experience of concurrent programming in Golang: from Goroutines to large-scale clusters

Introduction:
As computer architecture becomes more and more complex, the demand for concurrency is also getting higher and higher. As a programming language that emphasizes concurrency, Golang provides a powerful and concise concurrency model, making it easier for developers to write efficient concurrent programs. This article will introduce some insights into concurrent programming in Golang, from the basic usage of Goroutines to the practical application of large-scale clusters.

1. Basic use of Goroutines
Goroutines is a lightweight thread in Golang. Starting a Goroutines is different from traditional multi-threads. It will share the memory space in the same process, so opening and The cost of destruction is relatively low. The following is a simple sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    go task1()
    go task2()

    // 等待任务完成
    time.Sleep(2 * time.Second)

    fmt.Println("All tasks completed!")
}

func task1() {
    for i := 1; i <= 5; i++ {
        fmt.Println("Task 1 is running...")
        time.Sleep(500 * time.Millisecond)
    }
}

func task2() {
    for i := 1; i <= 5; i++ {
        fmt.Println("Task 2 is running...")
        time.Sleep(500 * time.Millisecond)
    }
}
Copy after login

In the above code, the main function opens two Goroutines and starts two tasks task1 and task2. After waiting for the completion of the two tasks through the time.Sleep function, "All tasks completed!" is output. By running the above code, you can see that the two tasks are executed concurrently and the output is in an alternating form.

2. Implement communication between Goroutines based on Channel
In Golang, Goroutines can communicate through Channel. Channel is a safe concurrent data structure that allows Goroutines to send messages safely. and receive data. The following is a sample code based on Channel:

package main

import (
    "fmt"
)

func main() {
    ch := make(chan int)

    go produce(ch)
    go consume(ch)

    // 等待任务完成
    select {}
}

func produce(ch chan<- int) {
    for i := 1; i <= 5; i++ {
        ch <- i
    }
}

func consume(ch <-chan int) {
    for i := 1; i <= 5; i++ {
        data := <-ch
        fmt.Println("Consumed data:", data)
    }
}
Copy after login

In the above code, the main function creates a Channel of type int and passes it as a parameter to the produce and consume functions. The produce function sends data through channel ch, while the consume function receives data through channel ch and outputs it. By running the above code, you can see that after the produce function sends the data to channel ch, the consume function immediately receives and outputs the data from channel ch.

3. Use Goroutines to improve program performance
Goroutines in Golang can achieve true parallel execution and can effectively improve the execution efficiency of the program. The following is a simple sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    start := time.Now()

    nums := []int{1, 2, 3, 4, 5}
    results := make(chan int)

    for _, num := range nums {
        go square(num, results)
    }

    total := 0
    for i := 0; i < len(nums); i++ {
        total += <-results
    }

    fmt.Println("Total:", total)
    fmt.Println("Execution time:", time.Since(start))
}

func square(num int, results chan<- int) {
    time.Sleep(1 * time.Second) // 模拟耗时操作
    results <- num * num
}
Copy after login

In the above code, the main function opens 5 Goroutines, and each Goroutines will send the square result of the input number to the results channel. The main Goroutine receives the data from the results channel through a loop and accumulates the results into the total variable. Finally, the sum and execution time of the program are output. By running the above code, you can see that the execution time of the program is significantly shortened due to the use of concurrent execution.

4. Practical application of large-scale clusters
In practical applications, Golang's concurrent programming model can be applied to the construction of large-scale clusters. The following is a simplified distributed crawler sample code:

package main

import (
    "fmt"
    "sync"
)

func main() {
    urls := []string{"https://www.example.com", "https://www.example.org", "https://www.example.net"}
    results := make(chan string)

    var wg sync.WaitGroup
    for _, url := range urls {
        wg.Add(1)
        go crawl(url, results, &wg)
    }

    go func() {
        wg.Wait()
        close(results)
    }()

    for result := range results {
        fmt.Println("Crawled:", result)
    }
}

func crawl(url string, results chan<- string, wg *sync.WaitGroup) {
    defer wg.Done()

    // 省略具体的爬取逻辑
    results <- url
}
Copy after login

In the above example, the main function creates a results channel of chan string type for receiving crawling results. Use sync.WaitGroup to wait for all Goroutines to complete. Each Goroutines will call the crawl function to implement specific crawling logic and send the results to the results channel. The main Goroutine obtains the crawling results by reading the results channel and outputs them.

Summary:
Through this article's introduction and practice of Golang concurrent programming, we have learned about the basic usage of Goroutines, Channel-based communication, the use of concurrency to improve program performance, and large-scale cluster practical applications. Golang's concurrent programming model allows developers to easily write efficient concurrent programs and provides convenience when building large-scale clusters. I hope this article can be helpful to the learning and practice of concurrent programming in Golang.

The above is the detailed content of Practical experience of concurrent programming in Golang: from Goroutines to large-scale clusters. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template