[Python] Unexpected behavir while iterating over two loops

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    Here is the Python3 code snipets for update_totps_loop and update_remaining_loop:

    def update_totps_loop(self):
    for i, (item_id, key, _) in enumerate(self.totp_rows):
    new_val = pyotp.TOTP(key).now()
    self.totp_tree.set(item_id, "code", new_val)
    # self.after(1000, self.update_totps_loop)
    vTimeTk_ns = time.time_ns()
    vUpdateTOTPS30s = (vTimeTk_ns / 30000000000)
    if vUpdateTOTPS30s - int(vUpdateTOTPS30s) > 0.5:
    vUpdateTOTPS30s += 1 + vUpdateTOTPS30s - int(vUpdateTOTPS30s)
    vNextTOTPUpdate = 30 - ((vUpdateTOTPS30s - int(vUpdateTOTPS30s)) * 30)
    self.vInt_RemainingSecs = int(vNextTOTPUpdate + 1)
    print(str(time.ctime()) + ",TOTP," + str(vTimeTk_ns) + "," + str(vUpdateTOTPS30s) + "," + str(int(vUpdateTOTPS30s)) + "," + str((vUpdateTOTPS30s - int(vUpdateTOTPS30s)) * 30) + "," + str(vNextTOTPUpdate) + "," + str(self.vInt_RemainingSecs))
    self.update_remaining_loop()
    self.after(int((vNextTOTPUpdate * 1000) + 1), self.update_totps_loop)

    def update_remaining_loop(self):
    self.remaining_label.config(text=str(self.vInt_RemainingSecs))

    print(str(time.ctime(time.time())) + ",BEFORE,,,,," + str((self.vInt_RemainingSecs)))
    self.vInt_RemainingSecs -= 1
    print(str(time.ctime(time.time())) + ",AFTER,,,,," + str((self.vInt_RemainingSecs)))

    if self.vInt_RemainingSecs > 0:
    self.after(1000, self.update_remaining_loop)


    And here is the terminal output:

    Thu Jul 24 22:19:48 2025,TOTP,1753375788873276457,58445861.25821844,58445861,7.746553122997284,22.253446877002716,23
    Thu Jul 24 22:19:48 2025,BEFORE,,,,,23
    Thu Jul 24 22:19:48 2025,AFTER,,,,,22
    Thu Jul 24 22:19:49 2025,BEFORE,,,,,0.1
    Thu Jul 24 22:19:49 2025,AFTER,,,,,-0.9
    Thu Jul 24 22:20:11 2025,TOTP,1753375811128115913,58445860.3709372,58445860,11.128115952014923,18.871884047985077,19
    Thu Jul 24 22:20:11 2025,BEFORE,,,,,19
    Thu Jul 24 22:20:11 2025,AFTER,,,,,18
    Thu Jul 24 22:20:12 2025,BEFORE,,,,,18
    Thu Jul 24 22:20:12 2025,AFTER,,,,,17
    Thu Jul 24 22:20:13 2025,BEFORE,,,,,17
    Thu Jul 24 22:20:13 2025,AFTER,,,,,16
    Thu Jul 24 22:20:14 2025,BEFORE,,,,,16
    Thu Jul 24 22:20:14 2025,AFTER,,,,,15
    Thu Jul 24 22:20:15 2025,BEFORE,,,,,15
    .
    .
    .


    update_remaining_loop is called from update_totps_loop after setting self.vInt_RemainingSecs to a number between 1 and 30 in update_totps_loop. The output lines having "TOTP" are from update_totps_loop and the lines having "BEFORE" and "AFTER" are from update_remaining_loop.

    Question is: Why is self.vInt_RemainingSecs getting set to -0.1 in the 2nd iteration of update_remaining_loop called in first iteration of update_totps_loop ?

    Continue reading...

Compartilhe esta Página