2

I would like to create a different task for every specific time and it will wait for the time and continue it day over day.

Below are my code but it only for one task.

import time, datetime

time_now = time.localtime(time.time())
now = []
now = time.strftime("%H:%M:%S", time_now)

value = []
value = time.strftime("15:00:00")

print(now)
print(value)

while value == now:
    time.sleep(1)
    print("\nSuccess")
keryazmi
  • 21
  • 1
  • 1
    I would look into cron jobs, and use those to run your python scripts. – Alex Nov 12 '18 at 07:46
  • 1
    You might consider something like `cron`,`launchd` or Windows Scheduler. Depending on what you have access to. Letting a single, ever running python script take care of a number of daily tasks for you is making things difficult for yourself, and probably less efficient on your CPU.. it would more sense to let these programs run your python scripts daily. unless you just want to do this for an exercise. – Zhenhir Nov 12 '18 at 07:48
  • 1
    This is a dup of at least two [other](https://stackoverflow.com/q/15088037/425458) [questions](https://stackoverflow.com/q/373335/425458). The consensus seems to be to use the `schedule` package. It's not a builtin, so you'll have to install it with `pip install schedule`. See the linked threads for details on use. – tel Nov 12 '18 at 07:58
  • Possible duplicate of [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) – tel Nov 12 '18 at 07:59
  • Thanks for the feedback. How do we extract the time information inside array and assign it to Cron? As for time in "value" in my code, it will be the variable. So the actual time will wait until it match the time variable and execute the task. – keryazmi Nov 12 '18 at 08:58

1 Answers1

0

I have implemented in my project and it's working properly. Please have a look on below code.

from django.http import HttpResponse, StreamingHttpResponse
from django.views.decorators.http import condition
import time

@condition(etag_func=None)
def stream(request):
    resp = StreamingHttpResponse(stream_response_generator(request))
    return resp

def stream_response_generator(request):
    start = time.time()
    end = start + 60
    while start < end:
        #Here you can add your method
        print ("This is test") # will print after 60 secound after frist executing of time
        time.sleep(2) # sleep time added
Anoop Kumar
  • 845
  • 1
  • 8
  • 19