同步长进程的最佳实践是什么? [英] what is the best practice for synchronous long-process?

查看:70
本文介绍了同步长进程的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在研究一个监控项目,它通过SNMP监控不同的目标,包括(交换机,路由器,PC,......)在特定的时间间隔内(大多数是60秒)。根据要监控的元素数量,某些监控过程需要很长时间,可能超过30秒。当目标数量超过2000并且大部分监控操作需要很长时间时,问题就会出现。这种情况监视操作overlap.it是因为显然没有可用的线程来分配它们,而cpu有四个核心。我试过下面的选项。

我发现监视器进入队列之间有差距当它真正开始执行时。事实上,(startMonitoringOperation - startMonitoring)=大间隔

这个大型交互可能超过2分钟。

我想知道什么同步长时间运行过程的最佳实践。如何减少我之前提到的差距。



Hi ,
I am working on a monitoring project that it monitor different targets including (Switch,Router,PC ,…) in their specific interval(mostly 60 seconds) via SNMP . depending on the number of elements that is going to be monitored, some of the monitoring processes take long time maybe more than 30 seconds .the problem appear when the number of targets are more than 2000 and majority of the monitoring operations take long time .in this occasion monitoring operations overlap.it is because apparently there is no available thread to allocate them while the cpu has four cores.i tried below options.
I found that there is gap between when monitor come into queue and when it really start to perform.in fact , ( startMonitoringOperation – startMonitoring ) = big interval
This big interavel could be more than 2 minute.
I want to know what is the best practice for synchronous long-running Process .How I can decrease the gap I have mentioned before.

for (int I = 0 ;MonitorsQueue.Count;i++) 
{
   Var startMonitoring = DateTime.Now;
   1.Threadpool.QueueUserWorkItem(new System.Threading.WaitCallback(DoMonitor),monitorObject);
   2.var task = new task(() => DoMonitor(monitorObject),TakeCreationOptions.LongRuning);
   3. var task = new task(() => DoMonitor(monitorObject));
}
Private void DoMonitor() //this Method take long in some occasions 
{
     Var startMonitoringOperation = DateTime.Now;
     //Monintoring Operation
}

推荐答案

听起来像线程池饥饿。根本没有足够的线程。



但是你必须要问,拥有2000个并发网络操作是否合理?我想在没有某种卸载安排的情况下,只要在内核中将CPU磨到静止状态,然后再进入代码。也许,取决于交通流量。



两种方法。您可以尝试增加线程池中的线程数,但我发现这个想法非常可怕。

Sounds like threadpool starvation. There simply aren't enough threads.

But you have to ask, does it makes sense to have 2000 concurrent network operations? I would think without some sort of offloading arrangement that'd be enough to grind the CPU to a standstill just in the kernel, before you even get to your code. Maybe, depending on the flow of traffic.

Two ways to go. You could try to increase the number of threads in the threadpool but I find this idea quite scary.
ThreadPool.SetMaxThreads();





更好的方法是切换到异步IO。这里的想法是你没有被阻塞等待网络活动的线程。相反,线程启动网络操作,然后重新使用以执行其他操作,直到响应返回,然后由另一个线程处理。



如果你想一想,网络连接是一种抽象 - 真正发生的是数据包流出适配器。



查看端口完成情况穿线。使用Begin / End方法和回调来实现实际的SNMP部分。



The better approach would be to switch to asynchronous IO. The idea here is that you don't have threads which are blocked waiting for network activity. Instead the thread starts the network operation and then gets reused to do other stuff until such time that the response comes back which will then be handled by a different thread.

If you think about it, a network connection is an abstraction - what really happens is flows of packets of data out of and into your adapter.

Have a look at Port Completion threading. Use Begin/End methods and callbacks to implement the actual SNMP part.


这篇关于同步长进程的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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