jczic / MicroWebSrv

A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
https://github.com/jczic/MicroWebSrv
MIT License
640 stars 115 forks source link

Feature - Fine tune control over processing #50

Closed washcycle closed 5 years ago

washcycle commented 5 years ago

I was thinking about creating a pull request to take the _serverProcess function and the ability to run the process outside in a while loop at the parent thread process.

def _serverProcess(self) :
    self._started = True
    while True :
        try :
            client, cliAddr = self._server.accept()
        except Exception as ex :
            if ex.args and ex.args[0] == 113 :
                break
            continue
        self._client(self, client, cliAddr)
    self._started = False

Proposed Addition

def serverProcessOneCycle(self) :
    try :
        client, cliAddr = self._server.accept()
    except Exception as ex :
        if ex.args and ex.args[0] == 113 :
            return
    self._client(self, client, cliAddr)

Example Usage

from microWebSrv import MicroWebSrv
mws = MicroWebSrv()                                    # TCP port 80 and files in /flash/www
mws.MaxWebSocketRecvLen     = 256                      # Default is set to 1024
mws.WebSocketThreaded       = False                    # WebSockets without new threads
mws.AcceptWebSocketCallback = _acceptWebSocketCallback # Function to receive WebSockets

mws.Init() ## not sure about this first init part

def some_process():
    #something awesome

whilte True:
    some_process()
    try:
        mws.serverProcessOneCycle()
   except:
       print("something went wrong")

Any thoughts, perhaps I didn't consider an already present micropythonic way to do this.

jczic commented 5 years ago

Hi, If you want to do that, without thread, you just can stop the server in the client connection callback event, no ? :)

washcycle commented 5 years ago

Is there any excessive overhead if I do that? Otherwise that sounds like an excellent idea, didn't even think of it. Thanks.

On Sun, Feb 3, 2019, 11:25 J-Christophe Bos notifications@github.com wrote:

Hi, If you want to do that, without thread, you just can stop the server in the client connection callback event, no ? :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv/issues/50#issuecomment-460070719, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBp_g0z8Nj0J5z4sFVTMBg-6ah5umomks5vJxtrgaJpZM4agJ-B .

jczic commented 5 years ago

No, because in any case, this is in the processing of the reception of the client socket that allows to close the server properly. The loop then comes out immediately (err #113) :) No problem.

jczic commented 4 years ago

Hello,

I released a fully new version (v2.0) of my web server here : github.com/jczic/MicroWebSrv2. Open source MIT, fully asynchronous, more robust, more fast and more efficient! It is delivered with a good documentation.

Thank you for your support and feedback. ☺️

Jean-Christophe Bos

Le lun. 4 févr. 2019 à 15:21, Matt notifications@github.com a écrit :

Is there any excessive overhead if I do that? Otherwise that sounds like an excellent idea, didn't even think of it. Thanks.

On Sun, Feb 3, 2019, 11:25 J-Christophe Bos notifications@github.com wrote:

Hi, If you want to do that, without thread, you just can stop the server in the client connection callback event, no ? :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv/issues/50#issuecomment-460070719, or mute the thread < https://github.com/notifications/unsubscribe-auth/ABBp_g0z8Nj0J5z4sFVTMBg-6ah5umomks5vJxtrgaJpZM4agJ-B

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv/issues/50#issuecomment-460266227, or mute the thread https://github.com/notifications/unsubscribe-auth/AAegLOFRwM78aMjfz91azlBS6lEsAwF0ks5vKEH_gaJpZM4agJ-B .

--

Sincèrement,