如何加快 ASP.NET 中调用命令行工具的循环速度?

发布于 2024-12-28 00:17:44 字数 440 浏览 0 评论 0原文

我有一个 ASP.NET 页面,它(快速)获取游戏服务器 IP 地址列表,并循环遍历它们,针对它们运行命令行工具以获取特殊的游戏服务器信息。我必须使用命令行工具,因为我不知道它是如何从机器获取信息的,而且我不想重新发明轮子。循环是缓慢的部分(惊喜)。每个命令行工具运行最多需要一秒钟,因此平均轮询大约 60 个 IP 地址时,页面加载可能需要 30-60 秒才能呈现我需要的结果。

我明显的想法是“多线程那个东西!”好吧,我尝试使用线程池,但如果一次有多个人访问该页面,最终会出现一个挂起的网站。一次仅使用 4-5 个调用,最多 60 个,因此加载时间为 10 秒。因此,它不仅在多个用户的情况下挂起,而且速度仍然太慢。如果我能把它控制在 3 秒以内,我会很高兴。

我应该提到这个页面位于共享托管环境中。我之前在共享托管环境之外有一个很好的解决方案,但我必须削减成本,现在我正在尝试使其在共享环境中工作。

还有希望吗?

I have an ASP.NET page that gets a list of game server ip addresses (quickly) and loops through them running a command line tool against them to get special game server information. I have to use the command line tool because I don't know how it works to get the information from the machines and I don't want to reinvent the wheel. The looping is the slow part (surprise surprise). Each command line tool run takes up to a second so with approximately 60 ip addresses polled on average, the page load can take from 30-60 seconds to render the results I need.

My obvious thought was "multithread that thing!" Well, I tried that w/ thread pools but ended up with a hanging website if more than one person accessed the page at a time. This was only using 4-5 calls at a time up to the 60 making it a 10 sec load time. So not only did it hang with multiple users, it was still too slow. I'd be happy if I could get it to under 3 seconds.

I should mention this page is in a shared hosting environment. I had a great solution before outside of the shared hosting environment but I had to cut costs and I'm trying to make it work w/ shared now.

Is there any hope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2025-01-04 00:17:44

您实际上不应该“按需”轮询这些服务器。最好使用 ASP.NET 来显示服务器信息列表,并使用其他进程(例如 Windows 服务或计划任务)每隔几分钟轮询一次服务器以生成该列表。总结一下:该服务将创建一个 XML 文件(例如),ASP.NET 将其显示给用户。这样,查看页面的用户数量不会影响您尝试轮询服务器的次数。

更新

您需要确保 ping 服务器的进程是单例的。具体来说,单例是一个只能存在单个实例的类。对于您的情况,更一般地说,您需要设置一个全局标志,表示“我当前正在 ping 服务器”,并设置另一个全局日期时间值,表示“我上次 ping 服务器的时间是在 hh:mm:ss” - 您可以使用应用程序字典来存储布尔标志和日期时间。每次有人加载您的页面时,请检查该标志以查看它是否已经对服务器执行 ping 操作。如果是,请不要这样做。如果标志显示“正常”,请检查当前时间与上次执行此操作的时间。如果少于5分钟,就不要这样做。所有这些都应该在后台线程中完成。该线程应该更新 App_Data 中的 xml 文件。对您页面的所有请求都应立即呈现此数据。页面请求永远不应该被阻塞。如果第一次调用时该文件不存在,则返回“ping 正在进行中,5 分钟后重试”。跟随?

在此处阅读有关 ASP.NET 应用程序状态字典的信息:
http://msdn.microsoft.com/en-us/library/ie /ms178594.aspx

You shouldn't really be polling these servers "on demand." It would be better to use ASP.NET to show the list of server information, and some other process - like a windows service, or scheduled task - to poll the servers every couple of minutes to generate that list. To summarize: The service would create an XML file (for example) and ASP.NET would display it to users. This way, the amount of users viewing the page does not affect the amount of times you try to poll the servers.

Update:

You need to ensure the process that pings servers is a singleton. Specifically, a singleton is a class in which only a single instance can exist. In more general terms for your case, you need to set a global flag that says "i'm currently pinging servers" and another global datetime value to says "the last time i pinged the servers was at hh:mm:ss" - you could use the Application dictionary to store the boolean flag and the datetime. Each time someone loads your page, check the flag to see if it's already pinging the servers. If it is, don't do it. If the flag says ok, then check the current time against the last time you did it. If it's less than 5 minutes, don't do it. All of this should be done in a background thread. This thread should update an xml file in App_Data. All requests to your pages should render this data immediately. A page request should never block. If the file is not there on the first call, then return "ping in progress, try again in 5 minutes." Follow?

Read about the ASP.NET Application state dictionary here:
http://msdn.microsoft.com/en-us/library/ie/ms178594.aspx

似梦非梦 2025-01-04 00:17:44

技术含量较低的解决方案可能是调用一个bat文件来调用每个exe,而不是从asp.net重复调用exe。将重复的 shell 保存到操作系统开销

每次调用 exe 都可以将结果传送到一个文本文件,一旦控制权从 bat 返回到 asp.net 应用程序,就可以立即读回该文本文件。

如果 IP 列表发生变化,则 ASP.NET 应用程序可以在运行之前创建 Bat 文件。

Low tech solution might be to call a bat file that makes each of the exe calls, instead of the exe repeatedly from asp.net. Saves the repeated shells to the OS overhead

Each call to the exe can pipe the results to a text file, which can then be read back all at once, once control returns to the asp.net app from the bat.

If the list of ip's change, then the the asp.net application could create the bat file before running it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文