问题
I've written a JavaScript-based Website that is able to enter, edit and solve Nonograms. As you may know, solving a nonogram is an NP-complete problem.
My first attempt was pure (single threaded) JavaScript. But on larger nonograms, Chrome showed its BSOD and killed the JS script after a few minutes. Next attempt was to use Web Workers. I split the solving algorithm so that each worker gets one row/column to solve and returns the result. This was an improvement and it was able to solve medium size nonograms. But still, sometimes the browser killed the JS VM showing the BSOD after some time plus the website was not really responsive as I would have expected since this is what Web Workers are made for, aren't they?
Just for "fun", I ported the solving algorithm to Python and used ajax requests calling a python script instead of Web Workers. Interestingly, it was even slower than JavaScript but after a some time of computation, the request returned a 500 Internal Server Error. I believe this is due to the maximum execution time of a CGI script which is 30s on PHP afaik.
The CGI idea was not the best because when multiple users want to solve a nonogram, the server runs on 100% CPU, so I probably stick with client side computation.
So question is, what is the best way to do this computation (which could take like 10min for larger nonograms)? I think the execution time is not an issue as long as the web site stays response and as long as the browser does not kill the execution tasks.
In the meantime, I'm also trying to optimize the recursive algorithm....
Thanks!
回答1:
Maybe you could add some delay before you post messages to the WebWorkers
. If the process is split up in to small enough functions, it may be possible to keep the page responsive, though it will take a lot longer to solve.
来源:https://stackoverflow.com/questions/9758999/how-to-handle-massive-computation-on-websites-web-workers-or-cgi