如何在 Nim 中编写异步代码以供定时器定期调用?
How to write async code in Nim to be called by timer periodically?
我需要每隔 500 毫秒定期将缓存保存到磁盘,我该怎么做?
我试过的代码编译不正确。此外,似乎 asyncCheck
应该用于错误检查。
import tables, os, asyncdispatch
var cache = init_table[string, int]()
proc save(cache: Table[string, int]): Future[void] {.async.} =
echo cache
addTimer(
timeout = 500, oneshot = false, cb = proc() = discard cache.save()
)
for i in (0..3):
cache["a"] = i
echo cache
sleep 1000
我认为这就是您想要的东西
import tables,asyncdispatch,strformat
#global, GC'd variable, careful!
var cache = initTable[string, int]()
proc save(){.async.} =
while true:
echo &"writing cache:{cache}"
await sleepAsync(500)
#just some fake data sources
proc write1(){.async.} =
await sleepAsync(900)
for i in 0..99:
cache["a"]=i
await sleepAsync(20)
#that happen at various rates
proc write2(){.async.} =
for i in 0..99:
cache["b"]=i
await sleepAsync(33)
#asyncCheck when we're not interested in the Future from a proc
#it will raise an Exception if the proc has an error
asyncCheck save() #save starts running asynchronously
echo "but we can still run other code"
waitFor: #blocks here until both write1 and write2 finish
write1() and write2()
echo "writing is done but save is still running"
cache["c"]=17
#os.sleep would block the whole thread, including save so instead
waitFor(sleepAsync(500)) #wait for save to echo one more time
echo "quit"
输出类似:
writing cache:{:}
but we can still run other code
writing cache:{"b": 15}
writing cache:{"b": 30, "a": 4}
writing cache:{"b": 45, "a": 29}
writing cache:{"b": 59, "a": 54}
writing cache:{"b": 74, "a": 78}
writing cache:{"b": 89, "a": 99}
writing is done but save is still running
writing cache:{"b": 99, "c": 17, "a": 99}
quit
我需要每隔 500 毫秒定期将缓存保存到磁盘,我该怎么做?
我试过的代码编译不正确。此外,似乎 asyncCheck
应该用于错误检查。
import tables, os, asyncdispatch
var cache = init_table[string, int]()
proc save(cache: Table[string, int]): Future[void] {.async.} =
echo cache
addTimer(
timeout = 500, oneshot = false, cb = proc() = discard cache.save()
)
for i in (0..3):
cache["a"] = i
echo cache
sleep 1000
我认为这就是您想要的东西
import tables,asyncdispatch,strformat
#global, GC'd variable, careful!
var cache = initTable[string, int]()
proc save(){.async.} =
while true:
echo &"writing cache:{cache}"
await sleepAsync(500)
#just some fake data sources
proc write1(){.async.} =
await sleepAsync(900)
for i in 0..99:
cache["a"]=i
await sleepAsync(20)
#that happen at various rates
proc write2(){.async.} =
for i in 0..99:
cache["b"]=i
await sleepAsync(33)
#asyncCheck when we're not interested in the Future from a proc
#it will raise an Exception if the proc has an error
asyncCheck save() #save starts running asynchronously
echo "but we can still run other code"
waitFor: #blocks here until both write1 and write2 finish
write1() and write2()
echo "writing is done but save is still running"
cache["c"]=17
#os.sleep would block the whole thread, including save so instead
waitFor(sleepAsync(500)) #wait for save to echo one more time
echo "quit"
输出类似:
writing cache:{:}
but we can still run other code
writing cache:{"b": 15}
writing cache:{"b": 30, "a": 4}
writing cache:{"b": 45, "a": 29}
writing cache:{"b": 59, "a": 54}
writing cache:{"b": 74, "a": 78}
writing cache:{"b": 89, "a": 99}
writing is done but save is still running
writing cache:{"b": 99, "c": 17, "a": 99}
quit