How does this package work? #203
Replies: 1 comment
-
welcome to Go and programming! Glad you've joined the party. And nope, no dumb questions, just opportunities to learn and understand better.
No it doesn't use crontab in Linux. It's using functions built into the language, specifically https://pkg.go.dev/time#AfterFunc which the go language then implements properly per OS.
The jobs are spawned into non-blocking go-routines and the don't run forever. Each job is scheduled in a go routine by You can check this out with a simple example:
You will see it print 3 generally, sometimes 4 given the timing of the goroutines being shutdown. 1 goroutine for the scheduler, 1 goroutine for the single job, and 1 goroutine for the executor. So, no it's not wasteful in terms of efficiency. Also, if you read up on goroutines, you'll find they're incredibly efficient and many thousands can be run on a single CPU core. Hopefully that helps you understand how it works overall and why your use case is completely reasonable for this package. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm new to Go (and programming in general) - I know my question does sound dumb, please bear with me.
I'm confused as to how does this package work? Does it depend on
crontab
in Linux - if so, does that not make it be OS-dependent? Does it spawn a new blocking go-routine for each job that will run forever, if so is that not wasteful in terms of efficiency?Also, how does this work (in general) compared to using
time.Ticker
in terms of scalability (creating hundreds of jobs that will run, say, once a day or less frequently)?For my use case, I want to be able to run multiple functions with varying delays - some would be running once every 15 minutes, some would run once in a few hours and some running once in a couple of days - creating goroutines, and blocking them for entire days at a time sounds very wasteful to me. Which is why I'm unsure how to proceed (without depending on
crontab
- and making my code be OS-dependent)Once again, sorry for the stupid question - I went through a part of Go docs, and tried to take a look at the code but still can't figure it out
Beta Was this translation helpful? Give feedback.
All reactions