Delayed Function Execution

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, sometimes we need to delay the execution of a function for a certain amount of time. This can be useful in scenarios where we want to wait for a certain event to occur before executing a function. In this challenge, you will be asked to create a function that delays the execution of another function by a specified amount of time.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/keyword_arguments("`Keyword Arguments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13619{{"`Delayed Function Execution`"}} python/keyword_arguments -.-> lab-13619{{"`Delayed Function Execution`"}} python/tuples -.-> lab-13619{{"`Delayed Function Execution`"}} python/function_definition -.-> lab-13619{{"`Delayed Function Execution`"}} python/lambda_functions -.-> lab-13619{{"`Delayed Function Execution`"}} python/importing_modules -.-> lab-13619{{"`Delayed Function Execution`"}} python/using_packages -.-> lab-13619{{"`Delayed Function Execution`"}} python/standard_libraries -.-> lab-13619{{"`Delayed Function Execution`"}} python/build_in_functions -.-> lab-13619{{"`Delayed Function Execution`"}} end

Delayed Function Execution

Write a function delay(fn, ms, *args) that takes a function fn, a time in milliseconds ms, and any number of arguments args. The function should delay the execution of fn by ms milliseconds and then invoke it with the provided arguments. The function should return the result of invoking fn.

To delay the execution of fn, use the time.sleep() function. This function takes a number of seconds as an argument, so you will need to convert ms to seconds before passing it to time.sleep().

from time import sleep

def delay(fn, ms, *args):
  sleep(ms / 1000)
  return fn(*args)
delay(lambda x: print(x), 1000, 'later') ## prints 'later' after one second

Summary

In this challenge, you were asked to create a function that delays the execution of another function by a specified amount of time. You learned how to use the time.sleep() function to delay the execution of a function and how to convert milliseconds to seconds.

Other Python Tutorials you may like