#Synchronous VS. Asynchronous programming
1 messages · Page 1 of 1 (latest)
There isn't a short or easy answer. As with most architectural decisions, it depends on weighing tradeoffs, and what you're trying to acheive.
everything that takes less than one second synchronous, everything else above 5 seconds asynchronous.
if you have no idea how long for the mean time, asynchronous
For browser ,ux, ui asynchronous
I wouldn't using timing as a guide for deciding about synch vs. asynch.
It really depends on what you're doing and what you're trying to achieve
if you have high throughput out of sync under 2 seconds asynchronous
for games, non network synchronous
Blanket guidelines like that rarely work well at scale
for database, synchronous thread
talking from experience
for message queue, semaphore locked asynchronous
In jdk8+, use non locking concurrent skip list map
simple calculation, logic synchronous
Just to make sure I understand what you said, your experience is that blanket guidelines that don't take into account any project (and coding) specifics are objectively the best solution every time?
I question the range, and depth of your experience then
you go async when it makes sense
Javascript nodejs makes everything wait asynchronous closure callback
you can do multi thread or multi process
depends on the language library also
networking is always wait async in c c++ js
if the library expects callback or function pointers then async
synchronous makes code simpler faster easier if possible
But if you stall everything for too long then bad idea
Alright... Cuz I'm looking to create a method or property for my python code to get a select set of methods for other usage and I wasn't sure if i should make it an async method or not...
I did actually read about this on Google but I wasn't sure.... So purely if the code takes a long time. What if the method takes between 1 and 5 min? (might be stupid but I'm curious)
1-5 minutes if it's not a database report, you should probably go async 😄
but in java / js / python, if you want to return a closure / fn pointer so that the user can modify the behavior then going that way is a good idea
this would be an observer / listener pattern or a polymorphic callback implementation
public abstract class AbstractController<INPUT, POJO, OUTPUT>
{
public abstract boolean validateInput(INPUT vo);
public abstract boolean validatePojo(POJO vo);
public abstract boolean validateOutput(OUTPUT vo);
public abstract void preProcessing();
public abstract void postProcessing();
}
so these would be "closures" / callbacks implemented by the caller,
sometimes with default implementations, this would make the code async but customizable.
Now the thing is you can make synchronous callback and asynchronous callback,
depends on your scenario or language.
In my case, those were synchronous callbacks, since I need to process everything before returning the HTTP payload.
If you were using HTTP/2 websocket, then those could be async network calls
In C, that's called a struct of function pointers callback, and it's often async
In Java, that's called a listener / observer / functional inner class or Functional Interface, could be sync or async
In JavaScript / NodeJS, that's called a closure and it's often async