Python Forum
Schedule a task and render/ use the result of the task in any given time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Schedule a task and render/ use the result of the task in any given time
#1
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 )
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 465 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Background task Mctweed 4 432 Feb-13-2024, 11:50 PM
Last Post: Mctweed
  How can I multithread to optimize a groupby task: davisc4468 0 707 Jun-30-2023, 02:45 PM
Last Post: davisc4468
  [SOLVED] Why is this asyncio task blocking? SecureCoop 1 770 Jun-06-2023, 02:43 PM
Last Post: SecureCoop
  Start print a text after open an async task via button Nietzsche 0 699 May-15-2023, 06:52 AM
Last Post: Nietzsche
  How to timeout a task using the ThreadpoolExecutor? lowercase00 2 2,483 Feb-07-2023, 05:44 PM
Last Post: deanhystad
  add interrupt for next task kucingkembar 0 769 Oct-07-2022, 12:15 PM
Last Post: kucingkembar
  count certain task in task manager[solved] kucingkembar 2 1,117 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  How to add another task? Makada 2 1,492 Nov-03-2021, 05:38 PM
Last Post: Makada
  Need some coding guidance for a task peny 5 2,164 Sep-27-2021, 02:02 PM
Last Post: peny

Forum Jump:

User Panel Messages

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