I need a function that would do a sleep for X milliseconds or seconds. I need to wait for an asynchronous task to complete so I'm polling a flag regularly, but I don't want to take the whole CPU in a while loop. Any idea ?
You can use ipcSleep(N) where N is a number of seconds (integer).
Alternatively, if the asynchronous task is running as a background process, launched with ipcBeginProcess(), you could use the data handler argument to ipcBeginProcess() to be asynchronously triggered when the child process produces output. That way you don't have to make the virtuoso session block...
In reply to Andrew Beckett:
Thank you both for these tips !
I noticed that the asynchronous function I was talking about is strangely refusing to execute at the same time as other SKILL code. I first call a function, responsible for the start up of the asynchronous task (it's a layout extraction by the way). That function returns quite fast, but the extraction continues in the background, and it sends me text to the ICFB during this time. Meanwhile, I can enter SKILL commands, like (plus 3 4), etc, and get answer from cadence. However, if I try to execute a loop with some delay in it (I found that (system "sleep 1") could also work for a delay), it seems the background task extracting the layout doesn't quite continue ! In fact, I have to stop my waiting loop for it to complete.. this is quite anoying as right now I am unable to run the layout extraction process as a command line instead of a SKILL command.
I guess the reason for this is that the SKILL interpreter must be single threaded. However, I REALLY need to execute the wait_function just once, and it must return when the background task is done.. which it won't do because the wait_function gets all the attention of cadence.... Any idea how I could break the circle ?
I hope I'm being a bit clear !
In reply to gsimard:
SKILL is in fact single threaded, as you have noted. However, in practice, a background process communicating via ipcBeginProcess will cause the data handler to get invoked at the first opportunity that the virtuoso SKILL toplevel is available - i.e. when the UI returns.
Functions such as ipcSleep and sleep will block virtuoso however.
I'm not sure why the background process would get held up, unless it is producing so much output that the output buffer is being filled because the foreground SKILL process is not able to process it?
It's very hard to tell without knowing the details of what you're doing...
Have you looked at the documentation for hiRegTimer that aplumb suggested? This command can issue a skill command to run some specified number of seconds in the future. It does not tie up the Skill interpreter. Simply define a checking function that will issue hiRegTimer to call itself again in the future, and terminate when your asyncrhonous task is finished.
hiRegTimer("someSkillCommand()" 100) ;This will run someSkillCommand in 10 seconds
Define the someSkillCommand to do hiRegTimer unless your threaded job is complete.
In reply to dmay:
Yes I have just done that.
This would work just fine, however, I really NEED a blocking function.
The keypoint is that I want to use the optimizer, and run an extraction between each step. What I was intending to do is to add a dummy goal in the optimizer that would do the extraction, and that command would really need to block the execution of the optimizer until the extraction is complete.
Right now I am thinking of just stepping the optimizer, from another function, then run the extraction, step the optimizer again, and so on, all that with a series of hiRegTimer calls.
Any simpler solution that could let me use the optimizer's full abilities without stepping it "manually" via skill code would be better though
EDIT: SorryAndrew I overlooked your second reply. The background "process" seems to require a communication with the SKILL interpreter at some key points, just for printing to the ICFB I think (I don't know the inner details of it though), which seems to be why it gets held if I have a function running.
A couple of thoughts. Are you using sleep() or ipcSleep()? If you use ipcSleep, then I think that doesn't block the data handler for ipcBeginProcess() - if my memory is correct (I can't test this right now). sleep however blocks everything... So that might give you the right combination of blocking yet allowing messages from the background process through.
As a side note, you might be interested in solution 11381132 (on sourcelink) which is a little package I wrote (based around hiRegTimer) which allows you to schedule repeating SKILL function calls, and provides you with a handle so you can manage these "processes" and kill them off when you want.
Well that ipcSleep function did exactly what I needed ! Blocking the top level execution but letting sub processes run.
Thank you !