continuous stream on IIS server - will it crash and how can i best execute this code?
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.
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.
精彩评论