我有一个cleanUp删除过时文件的后台任务 ( )。
func main() {
// 4 procs/childs max
runtime.GOMAXPROCS(4)
// start a cleanup cron-job
go cleanUp()
app := fiber.New(fiber.Config{
Prefork: true,
})
app.Post("/", handleFileupload)
log.Fatal(app.Listen(":4000"))
}
func cleanUp() {
fmt.Println("Cleaning Up..")
for {
// deletes old files here
time.Sleep(60 * time.Second)
}
}
目前cleanUp在所有 4 个进程上运行并打印Cleaning Up..4 次。
但我希望它只在单个进程上运行并打印单个Cleaning Up... 我已经尝试过 waitGroups 和 channels 但未能实现我想要的。
我怎样才能只在单个进程上运行它以避免竞争条件?
这是输出:
Cleaning Up..
┌───────────────────────────────────────────────────┐ ┌───────────────────────────────────────────────────┐
│ Fiber v2.38.1 │ │ Child PIDs ... 79378, 79379, 79380, 79381 │
│ http://127.0.0.1:4000 │ └───────────────────────────────────────────────────┘
│ (bound on host 0.0.0.0 and port 4000) │
│ │
│ Handlers ............. 5 Processes ........... 4 │
│ Prefork ........ Enabled PID ............. 79377 │
└───────────────────────────────────────────────────┘
Cleaning Up..
Cleaning Up..
Cleaning Up..
Cleaning Up..
千万里不及你
相关分类