Python Forum
Is using while True loop good?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is using while True loop good?
#3
(Jul-02-2020, 04:36 PM)HarleyQuin Wrote: It would constantly loop so whilst it doesn't seem like the code would use up much resource it doesn't seem sensible.
I'd at least add a time delay to loop every 5 minutes or something. time.sleep(300) will but 5 minutes between each check.

There are better ways to do this but i'm unsure as to what your overall goal is but sleep function should help.

Thank you for replying.
I've also thought about it.
5 minutes interval will be too large for my purpose.
1-5 millisecond will do.

To check approximate full speed I've used this code:
import time


def get_second_microsecond():
    """Returns current UTC second and microsecond as list"""
    gettime = time.time()
    second = int(gettime)
    microsecond = int(round((gettime - second), 6) * 1000000)
    timelist = [second, microsecond]
    return timelist


starttime = get_second_microsecond()
changingvariable = 0
comparingvariable = 1

for i in range(1000000):
    if changingvariable != comparingvariable:
        print('changing variable= {}, comparing variable= {}'.format(changingvariable, comparingvariable))
        comparingvariable = changingvariable
        print('changing variable= {}, comparing variable= {}'.format(changingvariable, comparingvariable))

endtime = get_second_microsecond()
totaldelay = (endtime[1] - starttime[1])
print('start time= {0[0]}.{0[1]}'.format(starttime))
print('end time= {0[0]}.{0[1]}'.format(endtime))
print('Total Delay= {} microsecond'.format(totaldelay))
Output:
changing variable= 0, comparing variable= 1 changing variable= 0, comparing variable= 0 start time= 1593730068.643423 end time= 1593730068.788341 Total Delay= 144918 microsecond Process finished with exit code 0
Grossly 7 million loops per second. will it not jam a part of the CPU?

if I put a 5 ms delay for 100 loops:
Output:
changing variable= 0, comparing variable= 1 changing variable= 0, comparing variable= 0 start time= 1593731128.280446 end time= 1593731128.816871 Total Delay= 536425 microsecond
Grossly 200 loops per second.
Hmm...
OK that might be an workaround.
But I am keeping this thread as unsolved if anyone have other explanation or other workaround.
Anyway, thanks again.
Reply


Messages In This Thread
RE: Is using while True loop good? - by HarleyQuin - Jul-02-2020, 04:36 PM
RE: Is using while True loop good? - by escape_freedom13 - Jul-02-2020, 11:16 PM
RE: Is using while True loop good? - by HarleyQuin - Jul-03-2020, 09:49 AM
RE: Is using while True loop good? - by buran - Jul-03-2020, 10:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While True loop help Nickd12 2 2,023 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  Do break operators turn while loop conditions from True to False? Drone4four 5 3,038 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  How to use while true/while loop on python christing 4 2,949 Oct-08-2019, 08:02 AM
Last Post: perfringo
  Returning True or False vs. True or None trevorkavanaugh 6 9,368 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 3,166 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 2,868 Feb-13-2019, 01:26 AM
Last Post: stullis
  Returning true or false in a for loop bbop1232012 3 8,231 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Get True of false outside a loop morgandebray 2 2,497 Aug-09-2018, 12:39 PM
Last Post: morgandebray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020