implementing setInterval/clearInterval in nim - nim-lang

I have the following code which run the passed proc in an interval, and return a clearInterval proc to stop the run, as follow:
proc runInterval(cb: proc, interval: int): Future[proc()] {.async.} =
var stop_run = false
while not(stop_run):
await sleepAsync(intv)
cb()
proc clearInterval() =
stop_run = true
return clearInterval
proc in_interval() =
echo "hahah"
let clearInterval = runInterval(in_interval, 1000) # run the in_interval proc every 1 second
clearInterval() # stop in_interval (can't compile)
runForever()
I suspect type annotation of runInterval is not correct as
: Future[proc()] {.async.}
, but I dont know how can I define a proc whose returnning type is proc as well as {.async.} ?

As #dom96 suggested you should make the closure return bool like so:
import asyncdispatch
import random
proc runInterval(cb: proc, interval: int) {.async.} =
var stop_run = false
while not stop_run:
await sleepAsync(interval)
if not cb():
break
proc in_interval(): bool =
if rand(1..6) > 2:
echo "lucky"
true
else:
echo "unlucky"
false
randomize()
discard runInterval(in_interval, 500)
runForever()

It's possible to implement this pattern by wrapping the while loop in its own async proc and removing the async from the runInterval proc to directly return clearInterval:
import asyncdispatch
proc runInterval(cb: proc, interval: int): proc() =
var stop_run = false
proc clearInterval() =
stop_run = true
proc runIntervalLoop() {.async.} =
while not(stop_run):
await sleepAsync(interval)
cb()
asyncCheck runIntervalLoop()
return clearInterval
proc in_interval() =
echo "hahah"
proc process {.async.} =
let clearInterval = runInterval(in_interval, 1000) # run the in_interval proc every 1 second
await sleepAsync(3000)
echo "Finished sleeping"
clearInterval()
echo "Cleared interval"
waitFor process()

Related

GODOT - how to disable player movement when dialog box is active and reenable it when its inactive

Ok, i'm really bad at coding. I'm especially new to GODOT and am trying to make a 2d game. I've been able to set cant_move to false when dialog is playing, the problem I'm having is making cant_move true again. I don't even know where to put done = true on the dialog script (I kinda just put it in a random place and hoped it would work). Here is my dog-shit code. (the solution is probably easy im just really dumb)
npc script
`extends Area2D
var done = true
func _ready():
Global.npc1 = self
func _exit_tree():
Global.npc1 = null
var can_interact = false
const DIALOG = preload("res://dialoguebox.tscn")
func _physics_process(delta):
$AnimatedSprite.play()
func diaplay():
if done == false:
Global.player.can_move = false
print("test")
if done == true:
print("test2")
can_interact = false
Global.player.can_move = true
func _on_Area2D_body_entered(body):
if body.name == "player":
$Label.visible = true
can_interact = true
func _on_Area2D_body_exited(body):
if body.name == "player":
$Label.visible = false
can_interact = false
func _input(event):
if Input.is_key_pressed(KEY_E) and can_interact == true:
done = false
diaplay()
$Label.visible = false
var dialog = DIALOG.instance()
get_parent().add_child(dialog)
dialog.position = $Position2D.global_position
dialog script
extends Control
var dialog = [
'sampletext',
'sampletext2',
]
var done = false
var dialog_index = 0
var finished = false
func _ready():
load_dialog()
Global.DialogBox = self
func _exit_tree():
Global.DialogBox = null
func _physics_process(delta):
$"Ind".visible = finished
if Input.is_action_just_pressed("ui_accept"):
load_dialog()
func load_dialog():
if dialog_index < dialog.size():
finished = false
$RichTextLabel.bbcode_text = dialog[dialog_index]
$RichTextLabel.percent_visible = 0
$Tween.interpolate_property(
$RichTextLabel, "percent_visible", 0, 1, 1,
Tween.TRANS_LINEAR, Tween.EASE_IN_OUT
)
$Tween.start()
if dialog_index >= 0:
Global.npc1.done = true
else:
queue_free()
done = true
dialog_index += 1
func _on_Tween_tween_completed(object, key):
finished = true
If I understand correctly, you are opening the UI with this code:
var dialog = DIALOG.instance()
get_parent().add_child(dialog)
dialog.position = $Position2D.global_position
So add the instruction to make can_move false there (Global.player.can_move = false).
And apparently it is all done here:
queue_free()
That is, the UI is removing it self. When the UI removes itself, it exits the scene tree, we are going to take advantage of that. Connect the tree_exited of the UI to a func that sets can_move true again:
Global.player.can_move = false
var dialog = DIALOG.instance()
dialog.position = $Position2D.global_position
dialog.connect("tree_exited", self, "dialog_exited")
get_parent().add_child(dialog)
func dialog_exited() -> void:
Global.player.can_move = true
That should do.
Alternatively you could create and emit a signal to notify when the player can move again. Refer to the documentation about signals.
Addendum
I think I got what else is not working. Look at load_dialog:
func load_dialog():
if dialog_index < dialog.size():
# ... some code ...
if dialog_index >= 0:
Global.npc1.done = true
else:
queue_free()
done = true
dialog_index += 1
The first check (dialog_index < dialog.size()) is if there is more dialog text. If there isn't then it is done. So change it to this:
func load_dialog():
if dialog_index < dialog.size():
# … some code …
else:
Global.npc1.done = true
queue_free()
done = true
dialog_index += 1
I hope that makes sense.
You also mention you got an error in Global.player.can_move = true, I suppose that happened with closing the game, that code ran, and Global.player was no longer valid.
Regardless of the situation, you can check if the player is valid:
var player = Global.player
if is_instance_valid(player):
player.can_move = true
I don't have enough rep to comment but I just want to add that you have two different done variables. One in NPC and another in Dialog. You set them true here:
if dialog_index >= 0:
Global.npc1.done = true
else:
queue_free()
done = true
I think the problem might also be that dialog's done is never set true again. But I'm a noob too and Theraot always give good advice so check his answer again.
In fact he already solved this problem:
if dialog_index < dialog.size():
# … some code …
else:
Global.npc1.done = true
queue_free()
done = true
dialog_index += 1
For clarity i would try to use only one done variable, probably connecting the scrips with signals also as Theraot said.

Why does removing a declared but not used variable causes program to terminate early?

I cannot figure this one out. When I remove let size = s.readInt64() from the following proc, the .exe seems to terminate before it reaches the end. It is declared but not used! Its gotta go!
proc load(fn: string): Alternating =
var s = newFileStream(fn, fmRead)
let size = s.readInt64() #WITHOUT THIS LINE THE .exe doesn't execute ExitTerminal()
result = newSeq[(float, int)]()
while not s.atEnd:
let element = (s.readFloat64.float, s.readInt64.int)
result.add(element)
s.close()
Below is the entire program. The file type is .dat and I suppose it is binary. It is created in the program. I compiled with -d:release Nim version 0.18.0 and minGW compiler
import streams
type
Alternating = seq[(float, int)]
proc store(fn: string, data: Alternating) =
var s = newFileStream(fn, fmWrite)
s.write(data.len)
for x in data:
s.write(x[0])
s.write(x[1])
s.close()
proc load(fn: string): Alternating =
var s = newFileStream(fn, fmRead)
let size = s.readInt64() #WITHOUT THIS LINE THE .exe doesn't execute ExitTerminal()
result = newSeq[(float, int)]()
while not s.atEnd:
let element = (s.readFloat64.float, s.readInt64.int)
result.add(element)
s.close()
let data = #[(1.0, 1), (2.0, 2)]
store("tmp.dat", data)
let dataLoaded = load("tmp.dat")
echo dataLoaded
### EXIT PROCEDURE FROM TERMINAL PROGRAM
proc ExitTerminal: bool =
echo "Exit Application"
echo "(y/n)"
while true:
case readline(stdin)
of "y", "Y", "yes", "Yes": return true
of "n", "N", "no", "No": return false
else: echo "Please be clear: yes or no"
if ExitTerminal() == false: discard ExitTerminal()
It is hard to tell because we know nothing about the file format you're reading. But generally you can't remove s.readInt64() just because it is unused, because apart from reading this proc advances your stream cursor. If you need to ignore the value just use discard s.readInt64() # Ignore size

Compile-time Call Count in Nim

The following code does not compile but illustrates what I would like to do: totalTests should hold the number of time that assertEquals() is called (assertEquals() should probably be a macro for this to be possible, but I'm not familiar with this aspect of Nim yet).
Any idea how this code should be modified to enable the following code to print [1/2] and [2/2] at the beginning of each test report line?
from strutils import format
var countTested = 0
var countFailed = 0
var countPassed = 0
let totalTests = 0 # <-- need let or other compile-time type here (?)
# using proc here... macro may be needed to be able to count calls (?)
proc assertEquals*[T](testName: string, expected: T, p: (proc(): T)) =
countTested += 1
totalTests += 1 # <-- compilation error (how can we increase each time it is called?)
write(stdout, format("[$num/$total] $name: ", "num", countTested, "total", totalTests, "name", testName))
var val = p()
if val == expected:
write(stdout, "passed\n")
countPassed += 1
else:
write(stdout, "failed\n")
countFailed += 1
when isMainModule:
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
Edit: added questions in code
Here is one way to do it. You can execute code at compile time by using a macro or a static statement. Note that there's still no way to reliably count these across multiple modules.
import macros, strutils
proc beginTests()
var countTested = 0
var countFailed = 0
var countPassed = 0
var totalTests = 0
var totalTestsCT {.compiletime.} = 0
macro endTests(): stmt =
quote do:
proc beginTests() =
totalTests = `totalTestsCT`
proc assertEqualsImpl*[T](testName: string, expected: T, p: (proc(): T)) =
countTested += 1
write(stdout, format("[$num/$total] $name: ",
"num", countTested, "total", totalTests, "name", testName))
var val = p()
if val == expected:
write(stdout, "passed\n")
countPassed += 1
else:
write(stdout, "failed\n")
countFailed += 1
macro assertEquals*[T](testName: string, expected: T, p: (proc(): T)): stmt =
totalTestsCT += 1
quote do:
assertEqualsImpl(`testName`, `expected`, `p`)
when isMainModule:
beginTests()
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
endTests()
An alternative implementation would be to embed the tests in a custom block statement, e.g.
testSuite:
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
The testSuite macro would then count the assertions in the embedded code and initialize the variable accordingly.
Yet another solution would be to not execute the tests directly, but store them in a list and only execute them at the end.
Here is an implementation of Reimer's third suggestion, which worked best for me.
import macros, strutils
type
TestSuiteObj = object
countTested: int
countFailed: int
countPassed: int
totalTests: int
tests: seq[(proc (self: TestSuite))]
TestSuite* = ref TestSuiteObj
proc newTestSuite*(): TestSuite =
new(result)
result.countTested = 0
result.countFailed = 0
result.countPassed = 0
result.totalTests = 0
result.tests = #[]
proc assertEquals*[T](self: TestSuite, testName: string, expected: T, p: (proc(): T)) =
self.totalTests += 1
var testProc = proc(self: TestSuite) =
self.countTested += 1
write(stdout, format("[$num/$total] $name: ", "num", self.countTested, "total", self.totalTests, "name", testName))
var val = p()
if val == expected:
write(stdout, "passed\n")
self.countPassed += 1
else:
write(stdout, "failed\n")
self.countFailed += 1
self.tests.add(testProc)
proc run*(self: TestSuite) =
self.totalTests = self.tests.len
for p in self.tests:
p(self)
var verdict = case (self.countTested == self.countPassed)
of true: "PASSED"
of false: "FAILED"
echo format("$verdict. Passed [$passed/$total] tests.", "verdict", verdict, "passed", self.countPassed, "total", self.countTested)
# Sanity
assert(self.countTested == (self.countFailed+self.countPassed))
assert(self.countTested == self.totalTests)
when isMainModule:
var suite = newTestSuite()
suite.assertEquals("test #A", 12, proc(): int = 14-2)
suite.assertEquals("test #B", 12, proc(): int = 12-2)
suite.run()

lua simple mutex for threaded app

I'm writing extension/plugin for some app.
In its documentation it's said, that there are 2 threads for plugin:
- "main thread" where all business logic must live
- "callback thread" where application invokes callbacks with predefined names by events, these callbacks must not do complicated things and return as soon as possible
documentation is not very clear so it's possible that callbacks are invoked not from one thread but from many threads.
I've wrote dummy mutex, that works like that:
Mutex = class {
_lock = function(self, owner)
assert(owner, 'owner must be set')
local ts = Timestamp.get()
local key = tostring(owner) .. ' ' .. tostring(ts)
self[key] = ts
for k, v in pairs(self) do
if k ~= key and v <= ts then
self[key] = nil
return false
end
end
return key
end,
lock = function(self, owner, wait)
local wait = wait or 0.01
local k
repeat
k = self:_lock(owner)
if k then return k else sleep(wait) end
until k
end,
unlock = function(self, key)
self[key] = nil
end
}
and use it for making thread safe queue like this:
ThreadSafeQueue = class {
new = function(cls)
return getmetatable(cls).new(cls, {
mx_queue = Mutex:new(),
mx_push = Mutex:new(),
})
end,
pop = function(self)
local lock_queue = self.mx_queue:lock(self)
local val
if #self then
val = table.remove(self, 1)
else
val = nil
end
self.mx_queue:unlock(lock_queue)
return val
end,
push = function(self, val)
if val == nil then return end
-- don't `push()` from few threads at the same time
local lock_push = self.mx_push:lock(val)
-- don't `pop()` when `push()` and `push()` when `pop()`
local lock_queue = self.mx_queue:lock(self)
self[#self + 1] = val
self.mx_queue:unlock(lock_queue)
self.mx_push:unlock(lock_push)
end
}
class here is helper that returns object with prototype lookups and :new() method which sets metatable.
Main problem is that I'm not sure what pairs() does.
- If original table will be modified while it's being iterated, will this loop return at least old state?
- Is it possible that some k, v will not be iterated in such case?
Other problem is that application I'm writing for is really black box and I'm not even sure on which os it will run (Win, Mac, Linux).
Everything that I know 100% that I have threads and socket module.
Could you review provided code?
Will it work?
Is there any other possibilities for mutex.
May be socket will give something?
option for socket:
try to create socket, if success, then mutex locked, else - wait it for close
local Mutex = class {
identities = {},
new = function(cls, identity)
assert(not cls.identities[identity])
local inst = getmetatable(cls).new(cls, {
port = identity,
server = nil
})
cls.identities[identity] = inst
return inst
end,
lock = function(self, wait)
local wait = wait or 0.01
local server
local ts = Timestamp.get()
repeat
server = socket.bind("*", self.port)
if server then
self.server = server
return true
else
sleep(wait)
end
assert(Timestamp.get() - ts < 3, 'deadlock')
until server
end,
unlock = function(self)
self.server:close()
self.server = nil
end
}

Groovy executing shell commands

Groovy adds the execute method to String to make executing shells fairly easy;
println "ls".execute().text
but if an error happens, then there is no resulting output. Is there an easy way to get both the standard error and standard out? (other than creating a bunch of code to; create two threads to read both inputstreams, then using a parent stream to wait for them to complete then convert the strings back to text?)
It would be nice to have something like;
def x = shellDo("ls /tmp/NoFile")
println "out: ${x.out} err:${x.err}"
Ok, solved it myself;
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ls /badDir'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout\nerr> $serr"
displays:
out> err> ls: cannot access /badDir: No such file or directory
"ls".execute() returns a Process object which is why "ls".execute().text works. You should be able to just read the error stream to determine if there were any errors.
There is a extra method on Process that allow you to pass a StringBuffer to retrieve the text: consumeProcessErrorStream(StringBuffer error).
Example:
def proc = "ls".execute()
def b = new StringBuffer()
proc.consumeProcessErrorStream(b)
println proc.text
println b.toString()
// a wrapper closure around executing a string
// can take either a string or a list of strings (for arguments with spaces)
// prints all output, complains and halts on error
def runCommand = { strList ->
assert ( strList instanceof String ||
( strList instanceof List && strList.each{ it instanceof String } ) \
)
def proc = strList.execute()
proc.in.eachLine { line -> println line }
proc.out.close()
proc.waitFor()
print "[INFO] ( "
if(strList instanceof List) {
strList.each { print "${it} " }
} else {
print strList
}
println " )"
if (proc.exitValue()) {
println "gave the following error: "
println "[ERROR] ${proc.getErrorStream()}"
}
assert !proc.exitValue()
}
I find this more idiomatic:
def proc = "ls foo.txt doesnotexist.txt".execute()
assert proc.in.text == "foo.txt\n"
assert proc.err.text == "ls: doesnotexist.txt: No such file or directory\n"
As another post mentions, these are blocking calls, but since we want to work with the output, this may be necessary.
To add one more important information to above provided answers -
For a process
def proc = command.execute();
always try to use
def outputStream = new StringBuffer();
proc.waitForProcessOutput(outputStream, System.err)
//proc.waitForProcessOutput(System.out, System.err)
rather than
def output = proc.in.text;
to capture the outputs after executing commands in groovy as the latter is a blocking call (SO question for reason).
def exec = { encoding, execPath, execStr, execCommands ->
def outputCatcher = new ByteArrayOutputStream()
def errorCatcher = new ByteArrayOutputStream()
def proc = execStr.execute(null, new File(execPath))
def inputCatcher = proc.outputStream
execCommands.each { cm ->
inputCatcher.write(cm.getBytes(encoding))
inputCatcher.flush()
}
proc.consumeProcessOutput(outputCatcher, errorCatcher)
proc.waitFor()
return [new String(outputCatcher.toByteArray(), encoding), new String(errorCatcher.toByteArray(), encoding)]
}
def out = exec("cp866", "C:\\Test", "cmd", ["cd..\n", "dir\n", "exit\n"])
println "OUT:\n" + out[0]
println "ERR:\n" + out[1]
command = "ls *"
def execute_state=sh(returnStdout: true, script: command)
but if the command failure the process will terminate

Resources