用延迟调用函数

问题描述 投票:0回答:2

我得到了这个练习面试的问题。

实现一个作业调度程序,它接受函数f和整数n,并在n毫秒后调用f。

我有一个非常简单的解决方案:

import time

def schedulerX(f,n):
    time.sleep(0.001*n)
    f

但是,建议的解决方案更加详细,如下所述。我不明白所有这些额外代码背后的目的是什么。请开明我。

from time import sleep
import threading

class Scheduler:
    def __init__(self):
        self.fns = [] # tuple of (fn, time)
        t = threading.Thread(target=self.poll)
        t.start()

    def poll(self):
        while True:
            now = time() * 1000
            for fn, due in self.fns:
                if now > due:
                    fn()
            self.fns = [(fn, due) for (fn, due) in self.fns if due > now]
            sleep(0.01)

    def delay(self, f, n):
        self.fns.append((f, time() * 1000 + n))
python multithreading job-scheduling
2个回答
0
投票

理论上存在一些差异。

我认为,第一个也是最重要的是,您的解决方案可以有效地一次只安排一个功能。因此,例如,假设你想从现在开始运行一个函数f1 10毫秒,之后再运行另一个函数f2 10毫秒。

你不可能轻易做到这一点,因为像schedulerX(f1, 10); schedulerX(f2, 10)这样的东西会等f1完成跑步才能开始等待f2。如果f1花了一个小时,你的f2安排将是完全错误的。

显然,第二个版本的目的是让计时器和每个函数在一个单独的线程中运行,这样一个函数调用就不会阻塞另一个函数。

然而,正如其他人在评论中指出的那样,导入是错误的,即使问题规范说明了一个函数,也需要一个list函数,并且它实际上并没有按照我描述的方式工作,所以或多或少,没有区别。


0
投票

正如其他人所指出的,你的解决方案是“阻止”:它可以防止在等待运行时发生任何其他事情。建议的解决方案的目的是让您安排工作,然后在此期间继续其他工作。

至于建议的代码正在做什么的解释:

你首先创建一个Scheduler,它将启动自己的线程,有效地在后台运行,并将运行作业。

scheduler = Scheduler()

在您的代码中,您可以安排所需的任何工作,而无需等待它们运行:

def my_recurring_job():
    # Do some stuff in the background, then re-run this job again
    # in one second.

    ### Do some stuff ###

    scheduler.delay(my_recurring_job, 1000)

scheduler.delay(lambda: print("5 seconds passed!"), 5 * 1000)
scheduler.delay(lambda: print("2 hours passed!"), 2 * 60 * 60 * 1000)
scheduler.delay(my_recurring_job, 1000)

# You can keep doing other stuff without waiting

调度程序的线程只是在其poll方法中永远循环,运行任何时间已到的作业,然后休眠0.01秒并再次检查。代码中有一个小错误,如果现在==到期,作业将无法运行,但也不会保留以供日后使用。它应该是if now >= due:而不是。

更高级的调度程序可能使用threading.Condition而不是每秒100次轮询:

import threading
from time import time

class Scheduler:
    def __init__(self):
        self.fns = [] # tuple of (fn, time)

        # The lock prevents 2 threads from messing with fns at the same time;
        # also lets us use Condition
        self.lock = threading.RLock()

        # The condition lets one thread wait, optionally with a timeout,
        # and lets other threads wake it up
        self.condition = threading.Condition(self.lock)

        t = threading.Thread(target=self.poll)
        t.start()

    def poll(self):
        while True:
            now = time() * 1000

            with self.lock:
                # Prevent the other thread from adding to fns while we're sorting
                # out the jobs to run now, and the jobs to keep for later

                to_run = [fn for fn, due in self.fns if due <= now]
                self.fns = [(fn, due) for (fn, due) in self.fns if due > now]

            # Run all the ready jobs outside the lock, so we don't keep it
            # locked longer than we have to
            for fn in to_run:
                fn()

            with self.lock:
                if not self.fns:
                    # If there are no more jobs, wait forever until a new job is 
                    # added in delay(), and notify_all() wakes us up again
                    self.condition.wait()
                else:
                    # Wait only until the soonest next job's due time.
                    ms_remaining = min(due for fn, due in self.fns) - time()*1000
                    if ms_remaining > 0:
                        self.condition.wait(ms_remaining / 1000)

    def delay(self, f, n):
        with self.lock:
            self.fns.append((f, time() * 1000 + n))

            # If the scheduler thread is currently waiting on the condition,
            # notify_all() will wake it up, so that it can consider the new job's
            # due time.
            self.condition.notify_all()
© www.soinside.com 2019 - 2024. All rights reserved.