Skip to content

Instantly share code, notes, and snippets.

@Enforcer
Created April 5, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Enforcer/c3a884c77b424977cb0d69893a60134b to your computer and use it in GitHub Desktop.
Save Enforcer/c3a884c77b424977cb0d69893a60134b to your computer and use it in GitHub Desktop.
@app.task(soft_time_limit=5, time_limit=10)
def time_limited_task():
"""
This task has time limits to ensure it will never work for
longer than anticipated.
soft_time_limit is a number of seconds when SoftTimeLimitExceeded
is raised to give a moment to clean up.
time_limit is a number of seconds after task is terminated unconditionally
"""
will_sleep_for = randint(5, 15)
logger.info(f'Work started (need {will_sleep_for}s to finish)')
try:
sleep(will_sleep_for)
except SoftTimeLimitExceeded:
logger.info('Oups, soft limit exceeded! Quickly, clean up!')
sleep(will_sleep_for)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment