I spent two days searching for the best way to convert asynchronous call into synchronous and the only one I found is polling. If you know a better way, I would very much appreciate that you shared. Also, what do you mean by “correct” polling?
Well that depends on your programming language. Remember that the apis can be called (when bindings are done) by many languages, so polling techniques are slightly diffenrent depending on the type of laguage used. So correct is dependent on the language used.
The principle in polling is to not waste system resources (particularly cpu).
The better way than polling is to hang an event on the finishing of the async function so that your process sleeps till the api returns. This is far better than polling. You could even do other things will waiting on the event. Even shell scripting “languages” (bash etc) allow you to wait till another child process completes.
Now to talk about async
With sync you are blocked till the api finishes and thus unable to do any other processing. With async you can return to your other processing while the api runs to completion.
Why do people prefer this. Well if your program is processing data from SAFE and does screen updates and waits for input from the user and so on, then a simple way is to have a state machine or master loop in which you perform certain tasks in an orderly fashion. Obviously this is just a simple form of a program. But it allows the master loop to continue running handling user input, screen updates etc while waiting for that file or MD object to be retrieved. Now with sync functions that master loop has to wait for the api to finish and the user gets a jittery experience. OR else you have to do code to essentially make the sync function into an async function.
Sorry, I didn’t write that it’s all about JavaScript.
I don’t really understand the concept, but thanks for an idea.
That’s true for long-running tasks. But there are many short tasks in SAFE API, that could easily be synchronous.
I think the solution would a option for the api to not return till its finished. Essentially making it a blocking (sync) api. In the api it simply puts the thread to sleep waiting for the finish event.
Assuming we’re still talking about JavaScript: JavaScript has no concept of blocking functions.
I think the issue that @loziniak is getting at, is that once you are using an asynchronous function (e.g. from the SAFE API), you have to make the calling functions asynchronous too. And so forth. This means even a call to the SHA256 function in the SAFE API to hash some data forces your routines to be asynchronous. Even when MaidSafe changes these functions to synchronous ones, the codebase will still potentially have traces of asynchronous calls.
This topic was automatically closed after 60 days. New replies are no longer allowed.