[Stackless] Monkeypatching

lars van Gemerden lars at rational-it.com
Tue Dec 17 10:39:17 CET 2013


Hi Andrew,

Does a poll_interval of 0.5 mean that the server is checking for new http
requests ever 0.5 seconds (seems long)?

It is pretty unpredictable how many tasklets are waiting to run while the
webserver is handling requests. Most likely there will be many, but any
that depend on IO (like sending an email) i would like to hand over to
 worker threads (eventually, not in the demo i am polishing right now), but
a substantial number of processes/tasklets have no IO at all. They are
started through channels, either fed by server requests or e.g. timers.

I also have timers running e.g. for processes/tasklets that are started
periodically. For now they are checking datetime.now() (or some such) every
time their tasklet is run, but in the future i need something better, at
least let all timers be handled by one tasklet in total.

With the tasklets.schedule() in the server_forever loop (keeping
poll_interval at 0.5) i occasionally (1 in 10 server starts) get the
following exception. Any ideas what might be the cause?

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 60867)
Traceback (most recent call last):
  File "C:\python27\lib\SocketServer.py", line 295, in
_handle_request_noblock
    self.process_request(request, client_address)
  File "C:\python27\lib\SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "C:\python27\lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\python27\lib\SocketServer.py", line 649, in __init__
    self.handle()
  File "C:\python27\lib\wsgiref\simple_server.py", line 121, in handle
    self.rfile, self.wfile, self.get_stderr(), self.get_environ()
  File "C:\python27\lib\wsgiref\simple_server.py", line 74, in get_environ
    env = self.server.base_environ.copy()
AttributeError: StacklessWSGIServer instance has no attribute 'server'
----------------------------------------

Cheers, Lars


On Thu, Dec 12, 2013 at 5:27 PM, Andrew Francis <andrewfr_ice at yahoo.com>wrote:

> Hi Lars:
>
> What I am suggesting is:
>
> >def serve_forever(self, poll_interval=0.5):
> >        self._BaseServer__is_shut_down.clear()
> >        try:
> >            while not self._BaseServer__shutdown_request:
> >                r, w, e = SocketServer._eintr_retry(select.select,
> [self], [], [], >poll_interval)
>
> write a function that does:
>
> if stackless.getruncount() > 1:
>     # we want to give tasklets that are ready to run a chance in case
>     # of  low I/O  activity
>     poll_interval = .5
> else:
>     poll_interval = some_other_large_value
> r, w, e = SocketServer._eintr_retry(select.select, [self], [], [],
> poll_interval)
>
> My thinking is you want to cut down on needless returns and expensive
> system calls.
>
> I am also assuming you don't have some timer function that depends on the
> polling...
>
> Cheers,
> Andrew
>
>
>   On Thursday, December 12, 2013 5:15 AM, lars van Gemerden <
> lars at rational-it.com> wrote:
>  what i have now (in relation to the default simpleserver use of
> bottle.py) is:
>
>
> #-------------------------------------------------------------------------------------
> from bottle import Bottle, ServerAdapter
> from wsgiref.simple_server import WSGIServer, make_server,
> WSGIRequestHandler
> import select, SocketServer
>
> class StacklessWSGIServer(WSGIServer):
>     def serve_forever(self, poll_interval=0.5):
>         self._BaseServer__is_shut_down.clear()
>         try:
>             while not self._BaseServer__shutdown_request:
>                 r, w, e = SocketServer._eintr_retry(select.select, [self],
> [], [], poll_interval)
>                 if self in r:
>                     self._handle_request_noblock()
>                 tasklets.schedule()
>         finally:
>             self._BaseServer__shutdown_request = False
>             self._BaseServer__is_shut_down.set()
>
> class MyWSGIRefServer(ServerAdapter):
>     server = None
>
>     def run(self, handler):
>         self.server = make_server(self.host, self.port, handler,
>                                   server_class=StacklessWSGIServer,
> **self.options)
>         self.server.serve_forever()
>
>     def stop(self):
>         self.server.shutdown()
>
> app = Bottle()
>
>
> def start_server(world_model):
>     config = world_model.config
>     manager = WebManager(world = world_model, secret =
> "jhbv235gfuhbvq2345hy3bo0")
>     setattr(sys.modules[__name__], "manager", manager)
>     server = MyWSGIRefServer(host=config.WS_HOST, port=config.WS_PORT)
>     tasklets.tasklet(app.run)(server = server, debug = config.WS_DEBUG)
>     return server
>
>
> #-------------------------------------------------------------------------------------
>
> I did this mainly by some reverse engineering, not by  really
> understanding the server code.
>
> Cheers, Lars
>
>
> On Thu, Dec 12, 2013 at 2:07 AM, Andrew Francis <andrewfr_ice at yahoo.com>wrote:
>
> Hi Folks:
>
> This is what I am suggesting. The reactor tasklet before calling the
> underlying select/poll system call should look at the scheduler queue. If
> there are runnable tasklets, then call select/poll with a timeout.
> Otherwise block indefinitely. The idea is if your application is consists
> of mostly I/O bound tasklets, they will be blocked on channels and will
> eventually be scheduled and run when there is I/O for them.
>
> I'll write a simple example to illustrate.....
>
> Cheers,
> Andrew
>
>
>   On Wednesday, December 11, 2013 7:00 AM, lars van Gemerden <
> lars at rational-it.com> wrote:
>  Hi Andrew,
>
> It wasn't blocked on IO, the server got stuck in it's mainloop (or similar
> name). I don't know much about webservers, WSGIservers etc, but what i also
> tried was putting a schedule() in this mainloop (which i think polls the
> socket). Is this what you suggest? It seems to work well, apart from the
> occasional weird AttributeError, which i'll have to look into. However
> others discouraged this approach, but i never quite understood why.
>
> Cheers, Lars
>
>
> On Fri, Dec 6, 2013 at 7:40 PM, Andrew Francis <andrewfr_ice at yahoo.com>wrote:
>
>  Hi Lars:
>
> Message: 1
> Date: Thu, 5 Dec 2013 22:07:20 +0100
> From: lars van Gemerden <lars at rational-it.com>
> To: The Stackless Python Mailing List <stackless at stackless.com>
> Subject: Re: [Stackless] monkeypatching
> Message-ID:
>     <CAP8kt8yLp1P46DeZfdBwkeTvWmbWFXbAra+MSxViXhuWXj0Fsw at mail.gmail.com>
> Content-Type: text/plain; charset="windows-1252"
>
> >(oh, i created a tasklet for the bottle microframework run() method, which
> >includes the mainn server loop()) ; I only called schedule() on each web
> >server request to be able to do anything outside the server mainloop.
> >However that had the effect that process steps where not executed as
> >long as the end user sent no requests (some processes do not have any
> >web component).
>
> I haven't had a chance to carefully look at your code. However from this
> description, it sounds like a thread/tasklet is blocked on I/O.  This will
> have the effect of blocking all tasklets in that thread. What you need to
> do is occasionally call a timeout from the underlying system call (for
> example, select()) and do a schedule() so other tasklets can run.
>
> Cheers,
> Andrew
>
> P.S - Yahoo's new and improved mail interface is killing me!
>
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless
>
>
>
>
> --
> ====================================
> Lars van Gemerden
> lars at rational-it.com
> +31 6 26 88 55 39
> ====================================
>
>
>
>
>
> --
> ====================================
> Lars van Gemerden
> lars at rational-it.com
> +31 6 26 88 55 39
> ====================================
>
>
>


-- 
====================================
Lars van Gemerden
lars at rational-it.com
+31 6 26 88 55 39
====================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20131217/73cba3a1/attachment.html>


More information about the Stackless mailing list