I want to run a periodic erlang process every 10ms (based on wall clock time), the 10ms should be as accurate as possible; what should be the right way to implement it?
相关问题
- Timer vs. repetitive background worker
- Is “new” in Erlang part of the official standard a
- how to create a keep-alive process in Erlang
- setInterval doesn't slow down on inactive tab
- Timers not running when screen is turned off / dev
相关文章
- How do I modify a record in erlang?
- Check active timers in Erlang
- Is the HPET directly accessible in Windows?
- undefined function maps:to_json/1
- How to convert datetime() to timestamp() in Erlang
- Blinking Button for Simon Says
- Timer Label not updated after switching views (swi
- Indy TIdTCPClient receive text
If you really want to be as precise as possible and you are sure your task will take less time than the interval you want it performed at you could have one long running process instead of spawning a process every 10ms. Erlang could spawn a new process every 10ms but unless there is a reason you cannot reuse the same process it's usually not worth the overhead (even though it's very little).
I would do something like this in an OTP gen_server:
Then start the gen_server like this:
As long as the gen_server is running (if it crashes so will the parent process since they are linked) the function
do_task/0
will be executed almost every 10 milliseconds. Note that this will not be perfectly accurate. There will be a drift in the execution times. The actual interval will be 10ms + time it takes receive the timer message, cancel the old timer, and start the new one.If you want to start a separate process every 10ms you could have the
do_task/0
spawn a process. Note that this will add additional overhead, but won't necessarily make the interval between spawns less accurate.My example was taken from this answer: What's the best way to do something periodically in Erlang?
If you want really reliable and accurate periodic process you should rely on actual wall clock time using
erlang:monotonic_time/0,1
. If you use method in Stratus3D's answer you will eventually fall behind.You can test in the shell: