Epoch time increasing at every loop after decimal

Currently I’m creating Discord bots for my guild. Right now I’m creating a cryptocurrency price bot because I’m one of the devs for a token. I’m using the scheduler plugin to schedule a job every minute (.00). Well, every 20 or so iterations my epoch time increases to 0.01. I’m thinking this is happening because of the time it takes to execute the code.

Screenshot of whats happening (Discord)

For example:

timeEpochNewTwo: 1694359920.641738

timeEpochNewTwo: 1694359980.7014513

timeEpochNewTwo: 1694360040.7538671

timeEpochNewTwo: 1694360100.7685063

timeEpochNewTwo: 1694360160.7908633

The price bot is supposed to show the price at the start of the second, but after about 20-30 loops it reaches .01, I need this to stay at .00.

Thank you,

Smitty Newton

I’m expecting the epoch time to stop increasing little amounts at a fraction of a decimal point. I need the discord bot to always execute and show .00 seconds.

Basically I have made post requests and below the post request I madeup for lost time.

def job():
    x = 0
    while x < 1:
        x = 1
        for key in apiKeys:
            timeEpochNewTwo = time.time()
            print('timeEpochNewTwo: ' + str(timeEpochNewTwo))
            timeEpoch = math.trunc(time.time())

            #MY POST REQUESTS ARE HERE

            #Code below makes up for lost time due to post requests
            timeEpochNew = time.time()
            print(timeEpochNewTwo - timeEpochNew + 60)
            time.sleep(timeEpochNewTwo - timeEpochNew + 60)
        x = 0
schedule.every().minute.at(":00").do(job)
while True:
    schedule.run_pending()
    time.sleep(1)

Calculate the expected time for the next minute.
Calculate time remaining til the next minute.
Sleep for the remaining time before executing job.

import time
import schedule
import math

def job():
    current_time = time.time()
    current_second = int(current_time % 60)
    # Calculate the time remaining until the next minute
    sleep_seconds = 60 - current_second

    # Sleep until the start of the next minute
    time.sleep(sleep_seconds)

    # Perform your cryptocurrency price fetching here
    timeEpochNewTwo = time.time()
    print('timeEpochNewTwo: ' + str(timeEpochNewTwo))

# Schedule the job to run every minute at the start of the minute
schedule.every().minute.at(":00").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

I think this should fix your problem

Leave a Comment