处理Golang中不同事件的最佳方法

huangapple go评论84阅读模式
英文:

best way to handle different events in golang

问题

我想在Go语言中实现一种方法来响应三种不同类型的事件:

  • 文本文件的更改(inotify)
  • 定时事件(计时器已结束)
  • 接收到Linux信号(SIGUSR1)

该软件应该无限期地等待空闲中的事件,如果接收到来自文件更改、定时器或信号的事件,则应调用相应的处理函数。
事件处理完成后,应继续等待事件。

我查看了通道,为每个事件使用单独的goroutine,但迄今为止没有找到符合我的要求的解决方案。

有人有建议或示例,可以告诉我如何以最佳方式在Go语言中实现这个功能吗?
非常感谢!

英文:

I want to implement in golang a method to react to three different kind of events:

  • changes in a text file (inotify)
  • Timerevents (timer has ended)
  • Linux signal received (SIGUSR1)

The sw should wait infinitely for events in idle, and if events from filechanges, timers or signals get received, the according handling-functions should be called.
After the eventhandling the waiting for events should continue.

I had a look at channels, where for each of the events
a separate goroutine get used, but found nothing so far that would fit my demands..

Does anybody have a suggestion or example how this could be done in golang the best way?
Thanks a lot!

答案1

得分: 2

文件系统通知

https://github.com/fsnotify/fsnotify

定时器事件

https://gobyexample.com/timers

信号处理

https://pkg.go.dev/os/signal

https://gobyexample.com/signals

^ syscall 库支持 SIGUSR1 信号。

英文:

Filesystem Notifications

https://github.com/fsnotify/fsnotify

Timer Events

https://gobyexample.com/timers

Signal Handling

https://pkg.go.dev/os/signal

https://gobyexample.com/signals

^ the syscall library supports SIGUSR1

huangapple
  • 本文由 发表于 2023年7月11日 20:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76662145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定