IIS 服务器上的连续流 - 它会崩溃吗?我怎样才能最好地执行此代码?
我目前正在开发一个 Twitter 流媒体网络应用程序,作为大学项目的一部分。我编写了使用curl 从twitter 进行流式传输并将数据写入sql server 2008 Express 数据库的代码。
ProcessStartInfo curl = new ProcessStartInfo();
Process process = new Process();
protected void Page_Load(object sender, EventArgs e)
{
curl.FileName = @"c:\program files\Curl\curl.exe";
curl.Arguments = "http://stream.twitter.com/1/statuses/sample.json -u username:password";
curl.UseShellExecute = false;
curl.RedirectStandardOutput = true;
process = Process.Start(curl);
Twitter_Stream(sender, e);
}
protected void Twitter_Stream(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
// Start curl process
using (process)
{
using (StreamReader reader = process.StandardOutput)
{
try
{
// create connection and open connection
conn = new SqlConnection(ConfigurationManager.AppSettings["strConnectionString"].ToString());
conn.Open();
// Post the output from curl to the queue.
// One line = one tweet in json format.
while (!reader.EndOfStream)
{
// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "save_stream";
string result = reader.ReadLine();
Message message = new Message(result);
JObject obj = JObject.Parse(message.Body.ToString());
/* I parse the obj here and exec query to save data
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{ /*DO some error logging here.*/}
finally
{
// close the connection
conn.Close();
Thread.Sleep(1000);
Twitter_Stream(sender, e);
}
}
}
}`
我的问题是,由于我在代码中放置了一个 while 循环,该循环将或不应该结束,这是否会导致服务器负载出现问题。连续循环会导致服务器崩溃吗?另外我应该用什么来代替? 任何帮助将不胜感激。
I am currently developing a twitter streaming web app as part of a College proj. I have written code that uses curl to stream from twitter and writes the data to a sql server 2008 express database.
ProcessStartInfo curl = new ProcessStartInfo();
Process process = new Process();
protected void Page_Load(object sender, EventArgs e)
{
curl.FileName = @"c:\program files\Curl\curl.exe";
curl.Arguments = "http://stream.twitter.com/1/statuses/sample.json -u username:password";
curl.UseShellExecute = false;
curl.RedirectStandardOutput = true;
process = Process.Start(curl);
Twitter_Stream(sender, e);
}
protected void Twitter_Stream(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
// Start curl process
using (process)
{
using (StreamReader reader = process.StandardOutput)
{
try
{
// create connection and open connection
conn = new SqlConnection(ConfigurationManager.AppSettings["strConnectionString"].ToString());
conn.Open();
// Post the output from curl to the queue.
// One line = one tweet in json format.
while (!reader.EndOfStream)
{
// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "save_stream";
string result = reader.ReadLine();
Message message = new Message(result);
JObject obj = JObject.Parse(message.Body.ToString());
/* I parse the obj here and exec query to save data
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{ /*DO some error logging here.*/}
finally
{
// close the connection
conn.Close();
Thread.Sleep(1000);
Twitter_Stream(sender, e);
}
}
}
}`
My question is, As i have put a while loop that will or should never end in my code, will this cause an issue on the server load. Will a continuous loop crash the server? Also what should I use instead?
Any help at all would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不会使服务器崩溃,因为 IIS 已经在监视这种情况。如果页面的执行时间大于当前阈值, IIS 将杀死该线程。
您不应将其设为网页,而应将其设为控制台应用程序。您可以在其中尽可能多地使用无限循环。
No, this will not crash the server because IIS is already watching for this case. If a page's execution time is greater than the currrent threshold, IIS will kill the thread.
Instead of making this a webpage, you should make this a console application. You can use infinite loops as much as you want in those.