webdevRefinery Forum: Is this a bad programming practice... - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Sole_Wolf 

  • Group: Members
  • Posts: 307
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java,SQL

Posted 04 August 2012 - 02:34 PM (#1)

Is this a bad programming practice...


A friend and I are disagreeing on how a python script should run.

He wants the script to run once a second in an infinite loop while I want the script to run once a minute via cron daemon and not in an infinite loop.

His reason being, he wants for the data that is sent to the server to be in real-time.

How much would performance be impacted if the script is in an infinite loop that uploads data to a server?

Thanks.
--Sole_Wolf
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5169
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 04 August 2012 - 03:09 PM (#2)

If you want it real time, look up daemons :)

http://code.activest...the-python-way/
Reserved.
2


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 05 August 2012 - 11:48 AM (#3)

View PostSole_Wolf, on 04 August 2012 - 02:34 PM, said:

A friend and I are disagreeing on how a python script should run.

He wants the script to run once a second in an infinite loop while I want the script to run once a minute via cron daemon and not in an infinite loop.

His reason being, he wants for the data that is sent to the server to be in real-time.

How much would performance be impacted if the script is in an infinite loop that uploads data to a server?

Thanks.

I'm not entirely sure that this is really a question of 'practice' in that both of the methods are okay at certain things. It would therefore be good practice to just choose the solution which is most suited to you.

Obviously we can't advise well given the lack of information you've given us. Does the data have to be sent in 'real-time'? In general, it won't anyway because web requests take time.

Performance wise, cron is probably better. When using the loop approach as long as you make the thread sleep for a while after every iteration it should be fine (otherwise it won't relinquish the cpu and you'll max out one of your cores).

Another thing to consider is what happens if the infinite loop approach crashes. Where with cron it'd just get run again the next minute (and assuming that doesn't crash again, you only lose a minutes data) with an infinite loop you'd have to have some way of restarting the script.

If you give us more information, we'll be able to be a bit more helpful.
0


User is offline lobabob 

  • Group: Members
  • Posts: 98
  • Joined: 21-March 10
  • LocationWouldn't you like to know?
  • Expertise:HTML,CSS

Posted 06 August 2012 - 10:55 PM (#4)

You could do an infinite loop that sleeps for a very short while (half a sec maybe? is that short enough?) and also have a cron job that checks (or runs a script which checks) whether the loop has crashed or not and which will restart the loop if it has crashed. Would that work?
"When the world goes mad, one must accept madness as sanity since sanity is, in the last analysis, nothing but the madness on which the whole world happens to agree." - George Bernard Shaw

Life = http://xkcd.com
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5169
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 06 August 2012 - 11:16 PM (#5)

View Postlobabob, on 06 August 2012 - 10:55 PM, said:

You could do an infinite loop that sleeps for a very short while (half a sec maybe? is that short enough?) and also have a cron job that checks (or runs a script which checks) whether the loop has crashed or not and which will restart the loop if it has crashed. Would that work?

monit or god (rbgod) are great tools to ensure a process continues running.
Reserved.
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3435
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 07 August 2012 - 09:18 PM (#6)

As Sephern said, both approaches have their advantages and disadvantages. Either one is fine in a lot of situations. It really depends on what the script is.

Using an infinite loop (daemon) is probably more flexible and I'd personally go with that approach instead of using cron. Cron is generally better for occasional/automated things rather than data processing.
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services