A timer (ticker) that can be used in the field (can be used anywhere)

How to use ticker when executing some processing regularly

func main() {
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for {
		select {
		case t := <-ticker.C:
			fmt.Println("Current time: ", t)
		}
	}
}

https://play.golang.org/p/Ho-mWf66l6Z

reference

application

Stop at the signal

func main() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs)
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for {
		select {
		case t := <-ticker.C:
			fmt.Println("Current time: ", t)
		case <-sigs:
			fmt.Println("done")
			return
		}
	}
}

https://play.golang.org/p/mNGWiLgcFvr

Combine with other channels

func main() {
	c := make(chan int, 1)
	go func() {
		for i := 0; i < 10; i++ {
			time.Sleep(500 * time.Millisecond)
			c <- i
		}
		close(c)
	}()
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for {
		select {
		case i, ok := <-c:
			if !ok {
				fmt.Println("done")
				return
			}
			fmt.Println("i = ", i)
		case t := <-ticker.C:
			fmt.Println("Current time: ", t)
		}
	}
}

https://play.golang.org/p/mjCcZ45U98B

Recommended Posts

A timer (ticker) that can be used in the field (can be used anywhere)
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
[Django] Field names, user registration, and login methods that can be used in the User model
Functions that can be used in for statements
I made a simple timer that can be started from the terminal
A collection of Numpy, Pandas Tips that are often used in the field
Basic algorithms that can be used in competition pros
A personal memo of Pandas related operations that can be used in practice
ANTs image registration that can be used in 5 minutes
I made a familiar function that can be used in statistics with Python
QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
Scripts that can be used when using bottle in Python
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
Make a Spinbox that can be displayed in Binary with Tkinter
Python standard input summary that can be used in competition pro
Make a Spinbox that can be displayed in HEX with Tkinter
Python standard module that can be used on the command line
Create a custom field where enum can be specified in choices
Understand the probabilities and statistics that can be used for progress management with a python program
About the matter that torch summary can be really used when building a model with Pytorch
About the matter that the re.compiled object can be used for the re.match pattern
Easy padding of data that can be used in natural language processing
I created a template for a Python project that can be used universally
Hide the warning that zsh can be used by default on Mac
A story that heroku that can be done in 5 minutes actually took 3 days
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
File types that can be used with Go
Building Sphinx that can be written in Markdown
The problem that the ifconfig command cannot be used
A solution to the problem that the Python version in Conda cannot be changed
I want to create a priority queue that can be updated in Python (2.7)
If "can not be used when making a PIE object" appears in make
Easy program installer and automatic program updater that can be used in any language
How to install a Python library that can be used by pharmaceutical companies
"Gazpacho", a scraping module that can be used more easily than Beautiful Soup
Python scikit-learn A collection of predictive model tips often used in the field
[Python3] Code that can be used when you want to cut out an image in a specific size
Convert images from FlyCapture SDK to a form that can be used with openCV
Summary of statistical data analysis methods using Python that can be used in business
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
Can be used in competition pros! Python standard library
[Django] About users that can be used on template
The story that sendmail that can be executed in the terminal did not work with cron
A mechanism to call a Ruby method from Python that can be done in 200 lines
List the classes that can be referenced by ObjCClass
Simple statistics that can be used to analyze the effect of measures on EC sites and codes that can be used in jupyter notebook
How to set up a simple SMTP server that can be tested locally in Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
[Python3] Code that can be used when you want to resize images in folder units
[Python] A program to find the number of apples and oranges that can be harvested
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Text analysis that can be done in 5 minutes [Word Cloud]
Is there a bias in the numbers that appear in the Fibonacci numbers?
Implement a thread that can be paused by exploiting yield
I investigated the pretreatment that can be done with PyCaret
Let's make a diagram that can be clicked with IPython
Evaluation index that can be specified in GridSearchCV of sklearn
I wrote a script that splits the image in two
Timer used in while loop