Getting started with co-process
If you have Python skills, coroutine is familiar with the association, understand goroutine minutes. Goroutine and coroutine Two words long so like, because it is the same thing. Do not understand is also OK, first look at the association process.
Greenlet:python framework, very primitive, is a purely manual co-process framework
From Greenlet import Greenletdef test1 (): print gr2.switch () print 34def test2 (): print Gr1.switch () Print 78gr1 = Greenlet (test1) GR2 = Greenlet (test2) #创建两个协程test1, test2, but did not start the Gr1.switch () #切换到gr1, That is, start the process Gr1
Create two co-processes (the Greenlet framework is called Greenlet), respectively, Test1,test2, but not two. Then the main coprocessor executes Gr1.switch (), the CPU execution is given to the coprocessor test1, outputs 12, then switches to the co-test2 output 56, then the CPU execution is then cut back to test1, output 34. Because this does not switch to test2,78 will not be output, the program ends.
The above is the start of the process, you can see Greenlet all the switching work must be performed by the code display, the framework does not automatically dispatch, so that Greenlet is a purely manual framework. There is a maximum difference between a co-process and Multithreading: If a co-process does not hand over the CPU using a switch, the other process cannot get the CPU. Inside a thread, there are a lot of threads, and when the thread gets the CPU, the switch in that thread comes in (gets the CPU) but does not have a switch out (to hand over the CPU) to get the CPU, and the other coprocessor can only wait for its switch to hand over the CPU usage.
Gevent is higher than the greenlet of the advanced framework, said before, Greenlet of the co-operation of Pure manual, original, then Gevent is advanced: Gevent will automatically create a main process, the level is high, we call the scheduler, It will automatically dispatch many other threads within the thread, when a certain process block, forcing back the CPU use, to the other non-blocking of the association, if all the other association is blocked, it will take the CPU use, polling other processes, to see who does not block, and then to whom. Gevent can allow programmers to write multi-threaded as the same way, switching, scheduling, the framework is done, and the interface is similar to multithreading, learning difficulty.
Goroutine
The former said that the Golang Goroutine Library is more advanced gevent, but also automatic scheduling, want to goroutine synchronization will use channel or other mutex, and multithreaded programming very similar.
Unbuffered channel can be used to synchronize Goroutine
The buffered channel can be used as a counting semaphore, which can be referenced in the previous Golang study article on the semaphore limit flow example
Sync is available in the sync package. Mutex or sync. Rwmutex Two kinds of mutual exclusion elements. The straightforward point is the lock, get the lock, release the lock so that the synchronization
Sync. Once is used to ensure that a bunch of goroutine execute the same function, only once, and other blocks until the end of the execution.
var a stringvar once sync. Oncefunc Setup () { a = "Hello, World"}func doprint () { once. Do (Setup) print (a)}func Threeprint () { go doprint () go doprint () go Doprint ()}
Although there were 3 Goroutine to execute setup, once. Do guarantee that only one goroutine will be executed once setup, the other goroutine wait for it to complete, and then print 3 times.