Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3 sched.scheduler计时器启动_Python_Python 3.x_Timer_Scheduler - Fatal编程技术网

Python 3 sched.scheduler计时器启动

Python 3 sched.scheduler计时器启动,python,python-3.x,timer,scheduler,Python,Python 3.x,Timer,Scheduler,以下是我们希望能够做到的: 安排许多调用,每个调用在我们启动计时器T后精确的毫秒数 设置其他内容以在另一个线程中运行 启动线程S并启动计时器T 我们怎样才能做到这一点 这是我的建议。实际上,我们期望调度程序中的“时间单位”。输入与调用调度程序有关。运行,但显然是与调用调度程序有关。输入。有了足够的事件,调度循环中调度在i=0和i=10**6时的作业之间就产生了时间差,更不用说设置线程所产生的时间差了 谢谢 您始终可以使用来实现自己的调度程序。下面是一个简单的调度程序示例,其中延迟与启动相关: i

以下是我们希望能够做到的:

  • 安排许多调用,每个调用在我们启动计时器T后精确的毫秒数

  • 设置其他内容以在另一个线程中运行

  • 启动线程S并启动计时器T

  • 我们怎样才能做到这一点

    这是我的建议。实际上,我们期望
    调度程序中的“时间单位”。输入
    与调用
    调度程序有关。运行
    ,但显然是与调用
    调度程序有关。输入
    。有了足够的事件,调度循环中调度在i=0和i=10**6时的作业之间就产生了时间差,更不用说设置线程所产生的时间差了


    谢谢

    您始终可以使用来实现自己的调度程序。下面是一个简单的调度程序示例,其中延迟与
    启动相关:

    import heapq
    import time
    
    def fun(after, s):
        print('{} after {} seconds: {}'.format(time.time(), after, s))
    
    class Sched:
        def __init__(self):
            self.tasks = []
    
        def add_task(self, delay_seconds, priority, callback, args):
            task = (delay_seconds, priority, callback, args)
            heapq.heappush(self.tasks, task)
    
        def start(self):
            self.start_time = time.monotonic()
    
            while self.tasks:
                now = time.monotonic()
                delta = now - self.start_time
                nxt = self.tasks[0][0]
    
                if delta < nxt:
                    time.sleep(nxt - delta)
                else:
                    _, _, callback, args = heapq.heappop(self.tasks)
                    callback(*args)
    
    sched = Sched()
    sched.add_task(5, 1, fun, (5, 'second'))
    sched.add_task(5, 0, fun, (5, 'first'))
    sched.add_task(7, 0, fun, (7, 'third'))
    
    print(time.time(), 'start')
    sched.start()
    print(time.time(), 'end')
    

    请注意,这些任务并非完全在请求时运行。这是因为执行上一个任务的时间有限。

    我只想分享我们实际使用的解决方案。我用两种方法绘制了x=计划时间和y=实际执行时间的散点图:

    • 使用与尼米的答案类似的东西,使用
      time.sleep
    • 使用专用线程在不睡眠的情况下轮询:

      # where queue is a DeQueue that pops the earliest scheduled time first
      r = range(0, len(self._stimulus_list))
      t0 = monotonic()
      for _ in r:
          nxt = queue.get_nowait()
          while monotonic() - t0 < t_now: pass
          nxt.execute()
      
      #其中queue是首先弹出最早计划时间的出列
      r=范围(0,长度(自刺激列表))
      t0=单调的()
      对于r中的uu:
      nxt=queue.get_nowait()
      而单调()-t0
    第二种方法的斜率接近1.0,平均abs误差约为1μs(尽管存在一些预期偏差)。我们完全牺牲了一个线程,并使用了第二种方法


    我仍然觉得
    sched
    软件包很不直观。

    谢谢!但这是阻塞。sched也是吗?我总是比其他人更容易误解Python文档。@DouglasMyers Turnbull您可以在两种模式下使用它,默认为阻塞。
    # where queue is a DeQueue that pops the earliest scheduled time first
    r = range(0, len(self._stimulus_list))
    t0 = monotonic()
    for _ in r:
        nxt = queue.get_nowait()
        while monotonic() - t0 < t_now: pass
        nxt.execute()