I tried to make a mechanism of exclusive control with Go

Overview

This is my first and second post as an engineer. I had the opportunity to write an exclusive control mechanism in Go, so I will record it. This time, we will create an exclusive control mechanism using two methods.

Various tools

scenario

This time, using a table as a shared resource, when processing is performed on the table from multiple processes, a mechanism is created to prevent subsequent processes from operating the table until the previous process is completed. I will continue.

First, try updating the table at the same time using Goroutine without exclusive control. (sync.WaitGroup is for waiting the main goroutine. For more information here.)

var w sync.WaitGroup

func main() {
	w.Add(2)
	go update()
	go update()
	w.Wait()
}

func update() {
	defer w.Done()
	for i := 0; i <= 10; i += 5 {
		fmt.Println("tbl update:", i*10, "%")
		time.Sleep(time.Second)
	}
}

// tbl update: 0 %
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 50 %
// tbl update: 100 %
// tbl update: 100 %

You can see that the two processes are updating the table at the same time. This time, we will create a mechanism to obtain the following output so that the subsequent update will be performed after the previous update is completed.

// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %

Pattern using sync.Mutex

First, from sync.Mutex.

//The main function is the same as above

var m sync.Mutex

func update() {
	defer w.Done()
	defer m.Unlock()
	m.Lock()
	for i := 0; i <= 10; i += 5 {
		fmt.Println("tbl update:", i*10, "%")
		time.Sleep(time.Second)
	}
}
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %

Declare UnLock () and Lock () at the beginning of update (). Subsequent goroutines cannot apply Lock () until the preceding goroutines UnLock () (until the processing of the function is completed), and as a result, wait for the processing of the preceding goroutines to finish. You can see that the output is as you intended.

Although sync.Mutex is very simple and straightforward, it does only one process. It's a little different from this scenario, but it seems difficult to create a mechanism such as "to reduce the load on the server, prevent more than 1000 processes from running at the same time".

Pattern using semaphore

Since this is my first time using a semaphore, I will write about what a semaphore is.

What is a semaphore?

The semaphore is one of the exclusive control mechanisms and models the operation of railway tracks. (Semaphore means traffic light in Japanese) It was created to control the shared resources of railroad tracks so that they are not used at the same time. A semaphore consists of three semaphore variables, a P operation, and a V operation.

--Semaphore variables --Number of processes that can access shared resources ――It will not be negative --A semaphore variable that takes only 0 or 1 is called a ** binary semaphore **, and one that takes 0 to N is called a ** general semaphore **. --P operation --Decrement semaphore variables --A process secures shared resources --V operation --Increment the semaphore variable --A process releases shared resources

This scenario is a binary semaphore, and it seems that you need to perform a P operation at the beginning of update () and a V operation at the end.

Implementation

golang.org/x/syncを使って実装します。


const semaphoreCnt = 1

var s *semaphore.Weighted = semaphore.NewWeighted(semaphoreCnt)

func update() {
	defer w.Done()
	defer s.Release(1)           //V operation Increases the semaphore variable by 1.
	s.Acquire(context.TODO(), 1) //P operation: Decrease the semaphore variable by 1.
	for i := 0; i <= 10; i += 5 {
		fmt.Println("tbl update:", i*10, "%")
		time.Sleep(time.Second)
	}
}
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %
// tbl update: 0 %
// tbl update: 50 %
// tbl update: 100 %

golang.org/x/syncAcquire()がP操作に,Release()がV操作にあたります。 Exclusive control is realized by defining a semaphore variable with semaphore.Weighted and increasing or decreasing it usingAcquire ()andRelease ().

また、golang.org/x/syncでは、Acquire(),Release()でセマフォ変数をいくつ増減させるかを調節することが出来ます。(今回は2値セマフォのため、それぞれ1増減となっています。) This allows you to define the processing weights for each process.

golang.org/x/syncを使うことでsync.Mutexと比べ、より複雑な排他制御の仕組みを作ることができます。

reference

Recommended Posts

I tried to make a mechanism of exclusive control with Go
I tried to make a simple mail sending application with tkinter of Python
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make a Web API
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM
I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go
I want to make a game with Python
I tried to make a ○ ✕ game using TensorFlow
Python: I tried to make a flat / flat_map just right with a generator
I tried to make a calculator with Tkinter so I will write it
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to make a url shortening service serverless with AWS CDK
I tried to make a "fucking big literary converter"
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to draw a route map with Python
When I tried to make a VPC with AWS CDK but couldn't make it
[Patent analysis] I tried to make a patent map with Python without spending money
I tried to make a thumbnail image of the best avoidance flag-chan! With RGB values ​​[Histogram] [Visualization]
[Go + Gin] I tried to build a Docker environment
I tried to automatically generate a password with Python3
I tried to make an OCR application with PySimpleGUI
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to make a real-time sound source separation mock with Python machine learning
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to make creative art with AI! I programmed a novelty! (Paper: Creative Adversarial Network)
I tried to implement a volume moving average with Quantx
I tried to find the entropy of the image with python
I tried to make various "dummy data" with Python faker
I tried to find the average of the sequence with TensorFlow
I tried to automatically create a report with Markov chain
I want to make a blog editor with django admin
I tried to solve a combination optimization problem with Qiskit
I want to make a click macro with pyautogui (outlook)
I tried to get started with Hy ・ Define a class
I tried to implement ListNet of rank learning with Chainer
I tried to sort a random FizzBuzz column with bubble sort.
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried a stochastic simulation of a bingo game with Python
I tried to divide with a deep learning language model
I tried to make a simple text editor using PyQt
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!