-3

Code I have

def callback(message):
   if not message:
      Mailer().send_feed_error_report('Empty Message')
   if "message_type" not in message:
      Mailer().send_feed_error_report('"message_type" not in message')


class Mailer():
   def send_feed_error_report(self, error_info):
      send_mail(error_info)

callback(message)

send_mail will only send a mail. It is a simple function for sending mail.

I want to

  • send mail only after 1 hour
  • other functions/code has to run in parallel, so I'd like to avoid using time.sleep()
hc_dev
  • 8,389
  • 1
  • 26
  • 38
Dev_Shums
  • 1
  • 2

1 Answers1

2

Scheduling in Python

You can use threading.Timer to schedule a function to run after a specified time (delay).

See Python's Event scheduler module sched.

How it works

Passing the delay in seconds will start the thread after the given delay and execute it independent of the main thread. Hence does not block your main thread.

You can pass parameters to your function like a regular thread using args as third argument.

delay_by = 1 * 60 * 60 # in seconds
threading.Timer(delay_by, send_mail).start()

Source: Start a Function at Given Time

hc_dev
  • 8,389
  • 1
  • 26
  • 38
shoaib30
  • 877
  • 11
  • 24