多线程之延时哦

两个函数

  1. 简单点的
import sched
import threading
import time


def new_task(function, delay_time, args):
    scheduler = sched.scheduler(time.time, time.sleep)
    scheduler.enter(delay_time, 10, function, args)
    thread = threading.Thread(target=scheduler.run)
    thread.start()

    return thread

  1. 复杂一点的
import sched
import threading
import time

def async_exe(func, args=None, kwargs=None, delay=0):
    args = args or ()
    kwargs = kwargs or {}
    def tmp():
        func(*args, **kwargs)
    scheduler = sched.scheduler(time.time, time.sleep)
    scheduler.enter(delay, 10, tmp, ())
    thread = threading.Thread(target=scheduler.run)
    thread.start()

    return thread