Implemented non-blocking web server for #91 - #92
Conversation
There was a problem hiding this comment.
See my inline comment.
You are correct, lbl-srg/BuildingsPy#605 may have different causes than the ones addressed with #89. Looking at the whole code, my take is that proc.terminate() at line 472 is most likely the culprit for blocking execution.
I think #89 is still needed though. It clearly solves plotting issues in many cases as mentioned in lbl-srg/BuildingsPy#600 (comment). However, there are some remaining "weird" cases occurring erratically. This is the reason why #89 is still being considered as work in progress.
Maybe improving the termination of the web browser with a timeout and a fallback kill command could resolve it all. Another option would be to replace HTTPServer with https://docs.python.org/3/library/http.server.html#http.server.ThreadingHTTPServer:
This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.
I could work on that over the next days if that helps.
| if timeout < 0.00001: | ||
| worker = threading.Thread(target=wait_until(exit_test, timeout, 0.1, self.logger, *args)) | ||
| worker.start() |
There was a problem hiding this comment.
The target argument of threading.Thread should be:
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
But here target is assigned the returned value of wait_until, which is None. So worker.start() below actually starts nothing (wait_until has already been called with zero timeout at the time worker is declared).
I think that the correct syntax should be:
worker = threading.Thread(target=wait_until, args=(exit_test, float('inf'), 0.1, self.logger) + args)
worker.daemon = True # If the main program tries to exit, it won't wait for the `wait_until` monitoring threadBut even then, this doesn't seem to implement what's described in the issue:
to run the web server in a non-blocking thread if BuildingsPy requests a timeout=0
because the web server is launched separately with self.server_launch(), which already uses a non-blocking (daemon) thread:
def server_launch(self):
self.thread = threading.Thread(target=self.serve_forever)
self.thread.daemon = True # daemonic thread objects are terminated as soon as the main thread exits
self.thread.start()2ca9e4d to
85ee936
Compare
|
Most likely addressed with #89 |
This closes #91
This change is for lbl-srg/BuildingsPy#605
@AntoineGautier : While implementing it, I also saw #89 which is not yet merged and released. My testing does not include #89
Is #89 still needed? I am not sure if #89 actually solves the issue in lbl-srg/BuildingsPy#605 which is hard to reproduce as it only blocks sometimes, typically every 10th or 20th call to the web server.
We should decide if we should merge both, or only one of them, and then also make a funnel release so we can integrate it in BuildingsPy.