-1

Let's say that we have a main() function that invokes a few functions that have been placed into different files.

What's the best practice ?

Option 1 : Have the while loop into the main function

def main():
    interval = 60.0
    start_time = time.time()
    while True:
        # calls to our functions
        time.sleep(interval - (time.time()-start_time) % interval)

Option 2 : Having the main function called in the while loop

def main():
    # calls to our functions

interval = 60.0
start_time = time.time()
while True:
    main()
    time.sleep(interval - (time.time()-start_time) % interval)

Does it actually change anything to use one method rather than the other one ?

Thanks for your advices !

Pynoob
  • 5
  • 3
  • 3
    `What's the best practice?` Is usually a red flag for an off-topic question (opinion-based question), because I could say one thing but me peers could say something else, and no one would know who's right. – cs95 Aug 14 '17 at 07:29
  • 1
    Well for one using the first method you can reuse your logic. But @cᴏʟᴅsᴘᴇᴇᴅ is right, this question is unfortunately most likely off-topic. – Christian Dean Aug 14 '17 at 07:30
  • @Nullman conventions are generic, depending on exact use case, it may be better to use either of the methods. – shad0w_wa1k3r Aug 14 '17 at 07:31
  • @COLDSPEED You're right, I should have asked what method people would rather use and why (just like Spaces VS Tabs). In my case I need it to be fast, so the first option that creates less nodes when executed is more likely to be my last choice. Next time I'll be more carefull about what I'll ask. Thanks. – Pynoob Aug 14 '17 at 07:44

1 Answers1

1

For technical reasons, the first seems faster, but you won’t be able to see it.

It will be faster because it creates less nodes

The second case will be easier to read for a human

Which is better in most of the cases

Now, I can’t tell you which one you have to use, you’ll have to do it yourself because it depends on what you need.

Arthur Guiot
  • 713
  • 10
  • 25
  • Well, in my case I'm more interested in speed so it will be the first option I'll stay on. Thanks. – Pynoob Aug 14 '17 at 07:39
  • @Pynoob the "speed" improvement is very dependent on the use case, and in most cases will be negligible. – shad0w_wa1k3r Aug 14 '17 at 07:40
  • I do a request per minute to an API (it will be more soon), store the result which has the form "timestamp,data" into a specific file for each request and then plot the results in realtime. – Pynoob Aug 14 '17 at 07:51
  • @Pynoob If you want to create an API, believe me, Node.JS will be better in this case because it’s damn fast. Python is fast but it will never beat Node.JS. – Arthur Guiot Aug 14 '17 at 07:58