#Synchronous VS. Asynchronous programming

1 messages · Page 1 of 1 (latest)

tulip vigil
#

In an asynchronous projects, when should it be necessary/worth while to make a method asynchronous, and when is it redundant/not necessary? explanation or resources greatly appreciated

wooden onyx
#

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.

scarlet lantern
#

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

wooden onyx
#

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

scarlet lantern
#

if you have high throughput out of sync under 2 seconds asynchronous

#

for games, non network synchronous

wooden onyx
#

Blanket guidelines like that rarely work well at scale

scarlet lantern
#

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

wooden onyx
#

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?

scarlet lantern
#

anything that needs to wait, async that part

#

90% yes

wooden onyx
#

I question the range, and depth of your experience then

scarlet lantern
#

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

tulip vigil
#

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)

scarlet lantern
#

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.

scarlet lantern
#

If you were using HTTP/2 websocket, then those could be async network calls

scarlet lantern