如何处理网站上的大量计算?网络工作者还是CGI? [英] How to handle massive computation on websites? Web workers or CGI?

查看:92
本文介绍了如何处理网站上的大量计算?网络工作者还是CGI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个基于JavaScript的网站,该网站可以输入,编辑和解决 Nonograms 。如您所知,解决一个无图是一个NP完全问题。

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.

我的第一次尝试是纯JavaScript(单线程)。但是在较大的非图上,Chrome会在几分钟后显示其BSOD并杀死JS脚本。下一步尝试使用网络工作者。我拆分了求解算法,以使每个工作人员都可以得到一行/每列来进行求解并返回结果。这是一项改进,它能够解决中等大小的非图。但是,尽管如此,有时浏览器会在一段时间后杀死显示BSOD的JS VM,而且网站并没有像我期望的那样真正响应,因为这是Web Worker的目的,不是吗?

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?

只是为了好玩,我将求解算法移植到了Python,并使用了ajax请求而不是Web Workers来调用python脚本。有趣的是,它甚至比JavaScript还要慢,但是经过一段时间的计算后,该请求返回了500 Internal Server Error。我认为这是由于CGI脚本在PHP afaik上的最长执行时间为30秒。

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.

CGI的想法不是最好的,因为当多个用户想要解决时非图,服务器运行在100%CPU上,因此我可能坚持使用客户端计算。

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.

所以问题是,进行此计算的最佳方法是什么(较大的非图可能要花10分钟左右)?我认为只要网站保持响应并且浏览器不会终止执行任务,执行时间就不是问题。

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....

谢谢!

推荐答案

也许您可以增加一些延迟,然后再将消息发布到 WebWorkers 。如果将过程分解为足够小的功能,则可能可以使页面保持响应状态,尽管解决该过程将花费更长的时间。

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.

这篇关于如何处理网站上的大量计算?网络工作者还是CGI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆