HttpWebRequest和HttpWebResponse理想异步缓冲区大小 [英] HttpWebRequest and HttpWebResponse ideal async buffer sizes

查看:403
本文介绍了HttpWebRequest和HttpWebResponse理想异步缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用HttpWebRequest和HttpWebResponse在.NET 3.5中运行它们异步:BeginGetRequestStream,EndGetRequestStream,BeginWrite,EndWrite,BeginGetResponse,EndGetResponse,的BeginRead,EndRead - 处理请求所有的部件都是异步的。

I'm trying to use HttpWebRequest and HttpWebResponse in .NET 3.5, running them in asynchronously: BeginGetRequestStream, EndGetRequestStream, BeginWrite, EndWrite, BeginGetResponse, EndGetResponse, BeginRead, EndRead - all parts of handling a request are asynchronous.

我有发送大量并发请求的线程数。 EndRead和EndWrite都是阻塞操作 - 他们阻止当前线程同时对数据流的实际的读/写操作完成时,我试图拿出一个理想的输入/输出缓冲尺寸进行这些操作。

I have a few threads that send a large number of concurrent requests. EndRead and EndWrite are both blocking operations - they block the current thread while the actual read/write against the stream is done, I'm trying to come up with an ideal input/output buffer size for these operations.

我的理由是这样的:我有一次积极的多个请求,他们会继续射击回调,让线程知道有一些可用数据或数据被发送。如果我的缓冲区大,读/通过导线写入数据将花费更长的时间,所以EndRead / EndWrite将阻止更长的时间。这将迫使在同一线程上的其他请求等待的时间长一点,因为他们的通知将不得不等待,直到线程是畅通的。

My reasoning is this: as I have multiple requests active at a time, they'll keep firing callbacks to let the thread know there's some data available or data was sent. If my buffers are large, reading/writing the data through the wire will take longer, so EndRead/EndWrite will block longer. This would force the other requests on the same thread to wait a bit longer, since their notifications will have to wait until the thread is unblocked.

所以,我的问题是,这将是在这种情况下一个良好的读/写缓存的大小。我想每2048个字节,但我在不同的博客看到一些样品code显示完全不同的值。

So, my question is, what would be a good read / write buffer size in this situation. I was thinking 2048 bytes each, but some sample code I saw in various blogs show wildly different values.

在此先感谢您的任何想法。

Thanks in advance for any ideas.

推荐答案

我想一个更好的解决办法是不要担心缓冲区大小太大,但不阻塞线程。如果你传递一个委托给回调 *开头的参数方法,在操作完成时执行的回调你可以叫结束* 从那里,这将(几乎)立即返回。没有阻止必要的。

I think a better solution would be not to worry about the buffer sizes too much, but don't block the threads. If you pass a delegate to the callback parameter of the Begin* methods, that callback is executed when the operation completes and you can call End* from there, which will (almost) immediately return. No blocking necessary.

和有关缓冲区的大小,如果他们真的关系到你,你就应该剖析,并找出最适合于您的具体情况。

And regarding the buffer sizes, if they really matter to you, you should profile and find out what works best in your specific situation.

这篇关于HttpWebRequest和HttpWebResponse理想异步缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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