-->
Page 2 of 3

Re: Synchronous or asynchronous

PostPosted: Tue Feb 22, 2022 8:54 am
by Hairyloon
btidey wrote:I suspect the question was about asynchronous / synchronous operation of software elements rather than serial communication.


Yes, exactly this, thank you.

Say for example I have an asynchronous web server with a button on it.
The button should trigger a routine on the ESP.

Meanwhile the programme is running a loop.
Does the server interrupt the loop in order to run its routine?
If not, then how does it work?

Re: Synchronous or asynchronous

PostPosted: Tue Feb 22, 2022 5:15 pm
by btidey
For the synchronous web server case you will normally have a call (server.handleClient(); within the loop which checks to see if there is any client requests to be handled. If there is then the code is expected to process the request and send back any response before returning control back to the rest of the loop. If no request is outstanding then the handleClient returns immediately. This works OK for many simple cases but the handling can be delayed by any prolonged activity in the rest of the loop.

For the asynchronous case nothing is required in the loop. The asyncwebserver is set up with event handlers and also uses the asyncTCP library. with this it can listen for connections in the background, parse and formulate the request requests and then triggers the appropriate event handler. This can allow the user loop to continue processing when there any spare time in the handling for example while responses are being transmitted.

For detail on how this works look at the readme at
https://github.com/me-no-dev/ESPAsyncWebServer

Re: Synchronous or asynchronous

PostPosted: Thu Mar 03, 2022 7:43 am
by Hairyloon
btidey wrote:For the asynchronous case nothing is required in the loop. The asyncwebserver is set up with event handlers and also uses the asyncTCP library. with this it can listen for connections in the background, parse and formulate the request requests and then triggers the appropriate event handler...


This may be clear after I've visited the link, but this seems to mean that the loop would stop and wait for the event. Is that correct?

Re: Synchronous or asynchronous

PostPosted: Thu Mar 03, 2022 10:17 am
by btidey
It is not waiting for the event, but when an event occurs the handler will effectively perform its task and on a single core processor like the esp8266 this will inevitably pause activity in the loop.