30秒学会 Python 片段 · 2018年8月11日

30秒学会 Python 片段 – delay.md


title: delay
tags: function,intermediate

Invokes the provided function after ms milliseconds.

Use time.sleep() to delay the execution of fn by ms / 1000 seconds.

代码实现

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