get-char is block. I need a non-blocking version - stanza

It seems that get-char from console
val c = get-char(STANDARD-INPUT-STREAM)
is blocking. Also, it starts to return only when the user hit Enter.
Is there a non-blocking version?

Related

Synchronous mode of Webdriver.io have problems with Promise

I write tests on CoffeeScript using Webdriver.io framework (Wdio testrunner) with sync mode enabled. According to the documentation, Webdriver.io commands should be executed in synchronous mode. However, an unexpected problem appears in the process of using the Promise.
We are considering the simplest test that finds an element on a page by selector and displays the text of the found element to the console.
Example 1 – code without promise
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
In this example commands of the Webdriver.io work correctly.
Example 2 - the code is in the constructor of the Promise
p = new Promise((resolve, reject) ->
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
resolve()
)
return p
If the commands are contained in the constructor of the Promise, then they are correctly executed too.
Example 3 - the code is in the block .then after returning the Promise
p = new Promise((resolve, reject) ->
resolve()
).then(() ->
browser.url('... URL ...')
a = $('... selector ...').getText()
console.log(a)
)
return p
The next error message shows in display: "$ (...). GetText is not a function" (Example 3). Apparently, the commands of the Webdriver.io begin to work asynchronously. Though I can use await keyword to process those Promises but I want to execute the code equally in the same way (synchronously) regardless of the location of the code (in the Promise or outside it).
Also switching to asynchronous mode occurs when command Await is used.
Example 4 (Example 1 code using the await keyword)
await console.log('123')
browser.url('... URL ...')
a = browser.$('... selector ...').getText()
console.log(a)
In this case for the correctly work of program it will be necessary to redo all the code, taking into account asynchronous processing.
As a solution, I can write all the tests asynchronously, but the code will become more complicated. Can I work with commands of the Webdriver.io synchronously even when using the Promise?
If you want to use Promise in your wdio test script which in sync mode, then you need to use browser.call() of wdio. More details on call: v5 and v4 .
Here you can find the sample code and more details on how a call is used: How to use 3rd party method that takes callback in webdriverio
Thanks,
Naveen

Parallel processing through tcl script

Need solution for parallel processing in tcl (windows).
I tried with thread, still not able to achieve desired output.
To simplify My requirement I am giving a simple example as following.
Requirement:
I want to run notepad.exe without effecting my current execution of flow. From main thread control should go to called thread, start notepad.exe and come back to main thread with out closing the notepad .
Tried:(Tcl script)
package require Thread
set a 10
proc test_thread {b} {
puts "in procedure $b"
set tid [thread::create] ;# Create a thread
return $tid
}
puts "main thread"
puts [thread::id]
set ttid [test_thread $a]
thread::send $ttid {exec c:/windows/system32/notepad.exe &}
puts "end"
Getting Output:
running notepad without showing any log.
when closing notepad application I am getting following output.
main thread
tid0000000000001214
in procedure 10
end
Desired output:
main thread
tid0000000000001214
in procedure 10
---->> control should go to thread and run notepad.exe with out effecting main thread flow.
<<-------
end
So kindly help to solve this issue and if appart from thread concept any other is there let me know.
You're using a synchronous thread::send. It's the version that is most convenient for when you want to get a value back, but it does wait. You probably should be using the asynchronous version:
thread::send -async $ttid {exec c:/windows/system32/notepad.exe &}
# ^^^^^^ This flag here is what you need to add
However it is curious that the exec call is behaving as you describe at all; the & at the end should make it effectively asynchronous anyway. Unless there's some sort of nasty interaction with how Windows is interpreting asynchronous subprocess creation in this case.

Node program with oriento does not exit after select

From within node.js I use the oriento module to access a OrientDB. In principle everything works fine, the only thing is the program dos not exit as it should. Here is the example:
const oriento = require("oriento");
const server = oriento({});
const db = server.use(dbname);
db.select("#rid").from("codelists").limit(1).scalar().then(function (result) {
console.dir(result);
}).finally(function () {
db.close();
server.close();
console.info("finished");
});
The programm executes the select, then the "then" and finally the "finally" clauses. All fine. But it does not exit. There seems to be something hanging on the event loop. But what? And how can I get rid of it? The problem actually is worse then just "press Ctrl-C" or have a process.exit() when everything is done, because the code is supposed to run within a unit test, where I cannot call exit. Any suggestions?
The problem is solved in the current master version of oriento. https://github.com/codemix/oriento/issues/170
You can use process._getActiveRequests() and process._getActiveHandles() to see what's holding the app open. They are undocumented but commonly used in this scenario.

node.js multithreading with max child count

I need to write a script, that takes an array of values and multithreaded way it (forks?) runs another script with a value from array as a param, but so max running forks would be set, so it would wait for script to finish if there are more than n running already. How do I do that?
There is a plugin named child_process, but not sure how to get it done, as it always waits for child termination.
Basically, in PHP it would be something like this (wrote it from head, may contain some syntax errors):
<php
declare(ticks = 1);
$data = file('data.txt');
$max=20;
$child=0;
function sig_handler($signo) {
global $child;
switch ($signo) {
case SIGCHLD:
$child -= 1;
}
}
pcntl_signal(SIGCHLD, "sig_handler");
foreach($data as $dataline){
$dataline = trim($dataline);
while($child >= $max){
sleep(1);
}
$child++;
$pid=pcntl_fork();
if($pid){
// SOMETHING WENT WRONG? NEVER HAPPENS!
}else{
exec("php processdata.php \"$dataline\"");
exit;
}//fork
}
while($child != 0){
sleep(1);
}
?>
After the conversation in the comments, here's how to have Node executing your PHP script.
Since you're calling an external command, there's no need to create a new thread. The Node.js runloop understands that calls to external commands are async operations, and it can execute all of them at the same time.
You can see different ways for executing an external process in this SO question (linked answer may be the best in your case).
However, since you're already moving everything to Node, you may even consider rewriting your "process.php" script to Node.js code. Since, as you explained, that script connects to remote servers and databases and uses nslookup (which you may not really need with Node.js), you won't need any separate thread: they're all async operations that Node.js excels at performing.

How do I run function in vala asynchronously when a button is clicked

I am just starting out with Vala and have hit a hurdle
When I try and run a large function on a button press it locks the entire app up until it is finished
How would I put a something like the following into a thread or give it an asynchronous callback?
var btn = new Gtk.Button();
btn.label = "Run something massive!";
btn.clicked.connect (() => {
Process.spawn_command_line_sync("gksudo apt-get update",
out ls_stdout,
out ls_stderr,
out ls_status);
btn.set_sensitive (false);
});
In Gtk+, there is only one thread that processes GUI events. If you want to do a background process, you can either create a thread or split the task up and processes it in the main loop. I recommend the latter.
For launching a process, consider GLib.Process.spawn_async. To know when the process exits, you will have to install a handler using ChildWatch.
The example for ChildWatch is likely what you want.

Resources