我正在运行12个线程。这些线程正在调用的函数没有对某个对象进行任何锁定。但是这些线程仍占用太多时间。(16分钟)。每个线程正在解析运行正常的xml文档如果单独运行。这个问题是因为某些原因相关的最大没有线程由快递版提供或某些阻止dn通过快递版。
每个线程调用的函数代码如下所示。 每个线程都有不同的rssfeed(urladdress)
public static class RssFileReader
{
public static Rss GetRssDocumentData(string rssFeed)
{
Console.WriteLine("thread in RssFileReader: " + Thread.CurrentThread.Name);
IFormatProvider culture = new CultureInfo("fr-FR", true);
Rss rssDocumentObject=new Rss();
XmlDocument documentObj = new XmlDocument();
try
{
documentObj.Load(HttpClient.GetWebResponse(@rssFeed, null, null, 1200000, @"http://www.sahil.com").GetResponseStream());
}
catch(Exception e)
{
e.Source = "RssFileReader:Loading xmldocument object";
throw;
}
try
{
XmlNodeList channelList = documentObj.GetElementsByTagName("channel");
for (int k = 0; k < channelList.Count; k++)
{
rssDocumentObject.ListOfChannel.Add(new Channel());
int noOfItemInChannel = -1;
//XmlNodeList itemList = channelList[k].ChildNodes;
for (int i = 0; i < channelList[k].ChildNodes.Count; i++)
{
switch (channelList[k].ChildNodes[i].Name)
{
case "item":
noOfItemInChannel++;
XmlNodeList xmlChildNodeOfItem = channelList[k].ChildNodes[i].ChildNodes;
//debugging
//Console.WriteLine("Thread Name in item in RssFileReader" + Thread.CurrentThread.Name);
rssDocumentObject.ListOfChannel[k].ListOfItem.Add(new Item());
for (int j = 0; j < xmlChildNodeOfItem.Count; j++)
{
switch (xmlChildNodeOfItem[j].Name)
{
case "title":
rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemTitle.InnerText = xmlChildNodeOfItem[j].InnerText;
break;
case "link":
rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemLink.InnerText = xmlChildNodeOfItem[j].InnerText;
break;
case "description":
rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemDescription.InnerText = xmlChildNodeOfItem[j].InnerText;
break;
case "pubDate":
try
{
string dateTimeTemp = xmlChildNodeOfItem[j].InnerText;
char[] splitCharArray = new char[1];
splitCharArray[0] = ' ';
string[] splitedDateTimeTemp = dateTimeTemp.Split(splitCharArray);
string RFC822 = "ddd,ddMMMyyyyHH:mm:ss";
rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemPubDate.PublicationDate = DateTime.ParseExact(splitedDateTimeTemp[0] + splitedDateTimeTemp[1] + splitedDateTimeTemp[2] + splitedDateTimeTemp[3] + splitedDateTimeTemp[4], RFC822, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
//exception not rethrown default date is assigned
catch (Exception e)
{
//Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);
}
break;
case "guid":
rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemGuid.InnerText = xmlChildNodeOfItem[j].InnerText;
break;
}
}
break;
case "title":
rssDocumentObject.ListOfChannel[k].ChannelTitle.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "description":
rssDocumentObject.ListOfChannel[k].ChannelDescription.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "link":
rssDocumentObject.ListOfChannel[k].ChannelLink.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "language":
rssDocumentObject.ListOfChannel[k].ChannelLanguage.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "pubDate":
try
{
string dateTimeTempForChannel = channelList[k].ChildNodes[i].InnerText;
char[] splitCharArrayForChannel = new char[1];
splitCharArrayForChannel[0] = ' ';
string[] splitedDateTimeTempForChannel = dateTimeTempForChannel.Split(splitCharArrayForChannel);
string formatStringForChannel = "ddd,ddMMMyyyyHH:mm:ss";
rssDocumentObject.ListOfChannel[k].ChannelPubDate.PublicationDate = DateTime.ParseExact(splitedDateTimeTempForChannel[0] + splitedDateTimeTempForChannel[1] + splitedDateTimeTempForChannel[2] + splitedDateTimeTempForChannel[3] + splitedDateTimeTempForChannel[4], formatStringForChannel, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
//exception not rethrown default date is assigned
catch (Exception e)
{
//Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);
}
break;
case "lastBuildDate":
try
{
string dateTimeTempForChannel = channelList[k].ChildNodes[i].InnerText;
char[] splitCharArrayForChannel = new char[1];
splitCharArrayForChannel[0] = ' ';
string formatStringForChannel = "ddd,ddMMMyyyyHH:mm:ss";
string[] splitedDateTimeTempForChannel = dateTimeTempForChannel.Split(splitCharArrayForChannel);
rssDocumentObject.ListOfChannel[k].ChannelLastBuildDate.LastBldDate = DateTime.ParseExact(splitedDateTimeTempForChannel[0] + splitedDateTimeTempForChannel[1] + splitedDateTimeTempForChannel[2] + splitedDateTimeTempForChannel[3] + splitedDateTimeTempForChannel[4], formatStringForChannel, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
//exception not rethrown default date is assigned
catch (Exception e)
{
//Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);
}
break;
case "docs":
rssDocumentObject.ListOfChannel[k].ChannelDocs.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "generator":
rssDocumentObject.ListOfChannel[k].ChannelGenerator.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "managingEditor":
rssDocumentObject.ListOfChannel[k].ChannelManagingEditor.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "webMaster":
rssDocumentObject.ListOfChannel[k].ChannelWebMaster.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
case "ttl":
rssDocumentObject.ListOfChannel[k].ChannelTtl.InnerText = channelList[k].ChildNodes[i].InnerText;
break;
}
}
}
}
catch(Exception e)
{
e.Source = "RssFileReader:Reading xml document object data to rss object";
throw;
}
Console.WriteLine(" Thread out RssFilereader :" + Thread.CurrentThread.Name);
return rssDocumentObject;
}
}
注意: - 调试时我知道在加载xml文档时需要花费时间。 webrequest对象的数量是否对系统有任何限制?
答案 0 :(得分:3)
如果您正在使用同一服务器的Web请求,那么默认情况下这些请求会被限制为两个连接。
然后你没有处理响应和响应流,这意味着它会等到收集垃圾以释放连接以供其他地方使用。将您的加载代码更改为:
try
{
// Removed unnecessary @ signs
using (WebResponse response = HttpClient.GetWebResponse(
rssFeed, null, null, 1200000, "http://www.sahil.com"))
using (Stream responseStream = response.GetResponseStream())
{
documentObj.Load(responseStream);
}
}
catch(Exception e)
{
e.Source = "RssFileReader:Loading xmldocument object";
throw;
}
您可能只需要关闭网络响应 - 通过关闭响应来处理流 - 但最好确定一下。
您仍然只能同时使用与同一服务器的连接的两个线程(默认情况下,对于非ASP.NET;您可以使用ServicePointManager.DefaultConnectionLimit
或connectionManagement
config file element自行更改)但至少那时你不需要等待连接空闲或等待垃圾收集。
答案 1 :(得分:0)
我从控制台应用程序运行它进行测试时需要时间。 因为调试pint语句而花费大量时间的原因是io阻塞。