GF Frame Grpool-high performance Goroutine pool

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Although goroutine in the go language is relatively lightweight compared to the system threads, the frequent creation and destruction of goroutine under high concurrency is also very stressful for performance loss and GC. Full reuse of the goroutine reduces the performance loss of goroutine creation/destruction, which is the grpool of goroutine for pooling encapsulation purposes. For example, in order to perform tasks in 100W, the use of goroutine requires the creation and destruction of 100W goroutine, while the use of Grpool may require only thousands of goroutine at the bottom to fully re-use to perform all tasks. After testing, the performance of Grpool in high concurrency is several to hundreds of times times higher than that of native Goroutine! And it also greatly reduces memory usage. Performance Test Report: http://johng.cn/grpool-performance/### method List ' Gofunc Add (f func ()) func Jobs () IntFunc setexpire (expire int) Func SetSize (size int) func size () Inttype Pool func New (expire int, sizes ... int) *pool func (P *pool) Add (f func ()) func (P *pool) Close () func (P *pool) Jobs () int func (P *pool) setexpire (expire int) func (P *pool) SetSize (size int) func (P *pool) Siz E () int ' ' through Grpool. The new method creates a goroutine pool, and the effective time for the goroutine in the given pool, in seconds * *, and the second parameter as a non-required parameter to limit the number of work goroutine in the pool, by default. It is important to note that tasks can be added to the pool without restrictions, but the goroutine of work can be limited. We can query the current number of work goroutine through the size () method and use the jobs () method to query the number of tasks to be processed in the current pool. At the same time, the size and goroutine validity of the pool can be dynamically changed at run time through the SetSize and Setexpire methods. At the same time, for ease of use, the Grpool package provides the default Goroutine pool, directly through the Grpool.Add to the default pool, the task parameter must be a **func () * * type function/method. # # # Use Example 1, use the default Goroutine pool, limit 10 work Goroutine perform 1000 tasks. Https://gitee.com/johng/gf/blob/master/geg/os/grpool/grpool1.go ' Gopackage mainimport ("Time" "FMT" "gitee.com/ Johng/gf/g/os/gtime "" Gitee.com/johng/gf/g/os/grpool ") func job () {time. Sleep (1*time. Second)}func Main () {Grpool. SetSize (Ten) for I: = 0; i < 1000; i++ {Grpool. ADD (Job)} gtime. SetInterval (2*time. Second, func () bool {FMT. PRINTLN ("Size:", Grpool. Size ()) fmt. Println ("Jobs:", Grpool. Jobs ()) return true}) the function of the task function in the program of Select {}} "is sleep 1 seconds, so that it can fully demonstrate the Goroutine quantity limit function. Among them, we used the gtime. The setinterval timer prints out the number of work Goroutine in the current default pool and the number of tasks to be processed every 2 seconds. 2, we look at a novice often error-prone example https://gitee.com/johng/gf/blob/master/geg/os/grpool/grpool2.go ' gopackage mainimport ("Fmt "Sync" "Gitee.com/johng/gf/g/os/grpool") func main () {wg: = sync. waitgroup{} for I: = 0; I < 10; i++ {WG. ADD (1) grpool. Add (func () {fmt. PRINTLN (i) WG. Done ()})} WG. Wait ()} "The purpose of our code is to print out 0-9 sequentially, but output after running: ' 10101010101010101010 ' isWhat is it? The execution results here are performed either with the Go keyword or grpool. The reason is that, for asynchronous thread/coprocessor, when a function performs an asynchronous registration, the function does not actually start executing (only the memory address of the variable i is stored in the Goroutine stack at the time of registration), and the function will read the value of the variable I once the execution is started. At this point the value of the variable i has been increased from 10. After the reason is clear, the improvement scheme is also very simple, that is, when registering the asynchronous execution function, the value of the variable i is also passed to get, or the current variable i is assigned to a temporary variable that does not change, use the temporary variable in the function instead of directly using the variable i. The improved example code is as follows: 1), using the Go keyword https://gitee.com/johng/gf/blob/master/geg/os/grpool/grpool3.go "' Gopackage mainimport (" FMT "" Sync ") func main () {wg: = sync. waitgroup{} for I: = 0; I < 10; i++ {WG. ADD (1) go func (v int) {FMT. Println (v) WG. Done ()} (i)} WG. After Wait ()} "executes, the output is: ' 9012345678 ' note that asynchronous execution is not guaranteed to execute in the order in which the function is registered, as in the following. 2), use temporary variable https://gitee.com/johng/gf/blob/master/geg/os/grpool/grpool4.go ' ' Gopackage mainimport ("FMT" "Sync" " Gitee.com/johng/gf/g/os/grpool ") Func main () {wg: = sync. waitgroup{} for I: = 0; I < 10; i++ {WG. ADD (1) V: = i grpool. Add (func () {fmt. Println (v) WG. Done ()})} WG. Wait ()} ' ' executes, the output is: ' ' 9012345678 ', you can see that when using Grpool for task registration, only the func () type parameter can be used, so you cannot register the value of the variable I in the task registration, Therefore, the value of the current variable I can only be passed in the form of a temporary variable. 670 reads  
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.