服务调用在主线程中工作,但在多线程时崩溃

发布于 2024-11-18 05:12:28 字数 815 浏览 2 评论 0原文

我的公司有一个应用程序,用于跟踪与托管在各种计算机上的网站相关的信息。中央服务器运行一个 Windows 服务,该服务获取要检查的站点列表,然后查询在这些目标站点上运行的服务以获得可用于更新本地数据的响应。

我的任务是对此流程应用多线程,以减少运行所有站点所需的时间(近 3000 个站点,依次运行大约需要 8 小时)。当服务不是多线程时,它会成功运行,但是当我将工作分散到多个线程时(现在使用 3 个线程进行测试,加上一个观察程序线程),出现了一个奇怪的崩溃,似乎源于对远程服务的调用应该提供数据。这是一个 SOAP/XML 调用。

当在测试服务器上运行时,该服务只是放弃并且没有完成其任务,但不会停止运行。当通过调试器(Dev Studio 2010)运行时,整个事情就会停止。我将运行它,几秒钟后它将停止调试,但不是因为它已完成。它不会抛出异常或给我任何类型的消息。通过断点,我可以走到它刚刚停止的地方。事件记录将我带到同一个地方。它停止在尝试从其他站点上的 Web 服务获取响应的代码行上。再说一次:它只在多线程时才这样做。

我发现一些信息表明连接数有限制,默认为 2。建议的解决方案是向 app.config 添加一些标签,但这并没有解决问题...

<system.net>
    <connectionManagement>
        <add address="*" maxconnection="20"/>
    </connectionManagement>
</system.net>

我仍然认为这可能是与允许的连接数有关,但我一直无法在网上很好地找到有关它的信息。我缺少什么简单的东西吗?任何帮助将不胜感激。

My company has an application that keeps track of information related to web sites that are hosted on various machines. A central server runs a windows service that gets a list of sites to check, and then queries a service running on those target sites to get a response that can be used to update the local data.

My task has been to apply multithreading to this process to reduce the time it takes to run through all the sites (almost 3000 sites that take about 8 hours to run sequentially). The service runs through successfuly when it's not multithreaded, but the moment I spread out the work to multiple threads (testing with 3 right now, plus a watcher thread) there's a bizarre crash that seems to originate from the call to the remote services that are supposed to provide the data. It's a SOAP/XML call.

When run on the test server, the service just gives up and doesn't complete it's task, but doesn't stop running. When run through the debugger (Dev Studio 2010) the whole thing just stops. I'll run it, and seconds later it'll stop debugging, but not because it completed. It does not throw an exception or give me any kind of message. With breakpoints I can walk through to the point where it just stops. Event logging leads me to the same spot. It stops on the line of code that tries to get a response from the web service on the other sites. And again: it only does that when multithreaded.

I found some information that suggested there's a limit to the number of connections that defaults to 2. The proposed solution is to add some tags to the app.config, but that hasn't solved the problem...

<system.net>
    <connectionManagement>
        <add address="*" maxconnection="20"/>
    </connectionManagement>
</system.net>

I still think it might be related to the number of allowed connections, but I have been unable to find information around it online very well. Is there something straightforward I'm missing? Any help would be much appreciated.

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

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

发布评论

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

评论(1

她如夕阳 2024-11-25 05:12:28
  1. 无论多么奇怪的崩溃都无法逃脱堆栈转储。尝试检查该转储,看看它是否指出了一些明显的功能。
  2. 您是否使用某些第三方工具或其他组件来进行实际的服务调用?如果是,请检查文档/联系编写它的人,以确认其组件是线程安全的。如果不是,那么您将面临艰巨的任务。 :)(我曾经在不安全的数据库上工作过,所以相信我,发现一些全局静态变量被抛出的情况并不罕见。)

最后,如果你100%确定这是由于多线程造成的,那么,放一个锁在你的工作线程中。最初说它覆盖了整个主 while 循环。从理论上讲,它不应该崩溃,因为即使它是多线程的,您也已经序列化了执行。
下一步是缩小线程的范围。比如说,里面有三个函数
main-while-loop ,例如 f1()、f2()、f3(),然后开始锁定 f2() 和 f3(),同时保持 f1 解锁...如果一切正常,则问题出在 f2 或 f3( )。
我希望你明白我的建议,

我知道这就像盲人猜象,但如果你的代码使用了很多没有充分记录的外部组件,那么这是你能做的最好的事情。

  1. No crash however bizarre will escape the stack-dump. Try going through that dump and see if it points out to some obvious function.
  2. Are you using some third party tool or some other component for the actual service call ? If yes, then please check the documentation/contact-the-person-who-wrote-it, to confirm that their components are thread safe. If they are not, you have large task ahead. :) (I have worked on DB which are not safe, so trust me it is not very uncommon to find few global static variables thrown around..)

Lastly if you are 100% sure that this is due multiple threads then, put a lock in your worked thread. Initially say it covers entire main-while-loop. Therotically it should not crash not as even though it is multi-threaded, you have serialized the execution.
Next step is to reduce to scope of the thread. Say, there are three functions in the
main-while-loop , say f1(), f2(), f3(), then start locking f2() and f3() while leaving f1 unlocked... If things work out, then problem is somewhere in f2 or f3().
I hope you got the idea of what I am suggest

I know this is like blind man guessing elephant, but that is the best you can do, if your code uses LOT many external component which are not adequately documented.

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