是否SignalR服务器发送活动执行线程占有为每个连接? [英] Does SignalR Server Sent Events implementation occupy thread for each connection?
问题描述
当我使用signalR这回退到ServerSentEvents,我可以在小提琴手到服务器打开的连接等待数据分块的看,它意味着一个线程在这个时候被占用的服务器上?
When I use signalR which falls back to ServerSentEvents, and I can see on fiddler an open connection to the server waiting for chunked data, does it mean a thread is being occupied on the server at this time?
如果是这样,这是不是很有效,因为据我看到它。
If so, it is not quite efficient as far as I see it..
例如,我不会在服务器上写这样code,阻止线程:
For example I would not write such code in the server that blocks a thread:
public ActionResult ServerSentEvents
{
DateTime currentdate = DateTime.Now;
Response.ContentType = "text/event-stream";
while (currentdate.AddMinutes(1) > DateTime.Now)
{
Response.Write(string.Format("data: {0}\n\n", DateTime.Now.ToString()));
Response.Flush();
System.Threading.Thread.Sleep(1000);
}
}
难道SignalR做一些事情更有效率些?
Could it be that SignalR does something more efficiently here?
推荐答案
这看起来像MVC控制器code不SignalR枢纽code
That looks like MVC Controller code not SignalR Hub code
反正SignalR支持.NET 4.5的异步框架,所以你可以从该方法返回一个任务来代替,而有一个后台线程处理等待状态,当一个任务等待1000毫秒,你会继续这项任务。
Anyway, SignalR supports the async framework in .NET 4.5 so you could return a Task from that method instead and have one background thread handling the wait state and when a task has waited 1000 MS you will continue that task.
您应该永远有 Thread.sleep代码
在requst线程,它完全杀死的可扩展性,而其之所以喜欢实体框架和ADO.NET框架支持异步操作(如数据库AI / O操作是就像一个 Thread.sleep代码
)
You should never ever have Thread.Sleep
in a requst thread, it kills scalability completely, and its the reason why Frameworks like Entity Framework and ADO.NET supports async operations (A I/O operation like Database is just like a Thread.Sleep
)
编辑:如果你没有任何理由要使用SignalR可以在MVC 3使用任务从MVC 4或AsyncController和更早版本。但我会建议SignalR,因为他们正在做的所有的管道为你
edit: If you for no reason want to use SignalR you can use Tasks from MVC 4 or the AsyncController in MVC 3 and earlier. But I would recommend SignalR since they are doing all the plumbing for you
这篇关于是否SignalR服务器发送活动执行线程占有为每个连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!