Python Forum

Full Version: Schedule a task and render/ use the result of the task in any given time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to schedule a task and use the result of the task while the schedule is being running.

For example, once in every minute I want to get the current time and assigned it to a variable call "now4". So the "now4" value should get updated every minute.

Then from "now4", I want to deduct a previously assigned datetime variable "custom_date_time". The user is going to check this difference by running this part of the code on his own.

So user should have the access to updated "now4" value to get the difference.
How can I get this done.

from datetime import datetime
import schedule
import time


def my_task():
    now4 = datetime.now()
    print(now4)
    return now4


schedule.every(1).minutes.do(my_task)

custom_date_time = datetime(2021, 5, 1, 17, 30, 29, 431717)



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

print(custom_date_time, "Time difference", now4-custom_date_time )
Not sure if this is what you want
import schedule, time
from datetime import datetime

def my_task():
    custom_time = datetime(2021, 5, 1, 17, 30, 29, 431717)
    now = datetime.now()
    print(f'Custom Time:{custom_time} Current Time: {now} Time Difference: {custom_time-now}')

schedule.every(1).minutes.do(my_task)

while True:
    schedule.run_pending()
    time.sleep(1)
Output:
Custom Time:2021-05-01 17:30:29.431717 Current Time: 2021-05-04 02:06:31.249154 Time Difference: -3 days, 15:23:58.182563 Custom Time:2021-05-01 17:30:29.431717 Current Time: 2021-05-04 02:07:31.312671 Time Difference: -3 days, 15:22:58.119046
(May-04-2021, 07:07 AM)menator01 Wrote: [ -> ]Not sure if this is what you want
import schedule, time
from datetime import datetime

def my_task():
    custom_time = datetime(2021, 5, 1, 17, 30, 29, 431717)
    now = datetime.now()
    print(f'Custom Time:{custom_time} Current Time: {now} Time Difference: {custom_time-now}')

schedule.every(1).minutes.do(my_task)

while True:
    schedule.run_pending()
    time.sleep(1)
Output:
Custom Time:2021-05-01 17:30:29.431717 Current Time: 2021-05-04 02:06:31.249154 Time Difference: -3 days, 15:23:58.182563 Custom Time:2021-05-01 17:30:29.431717 Current Time: 2021-05-04 02:07:31.312671 Time Difference: -3 days, 15:22:58.119046

Actually, user should be able to run following code when he wants. So it should be independent of the function defined and run with while loop.

print(f'Custom Time:{custom_time} Current Time: {now} Time Difference: {custom_time-now}')