ext.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:aschedule 作者: eightnoteight 项目源码 文件源码
def every_random_interval(job, interval: timedelta, loop=None):
    """
    executes the job randomly once in the specified interval.
    example:
            run a job every day at random time
            run a job every hour at random time
    :param job: a callable(co-routine function) which returns
                a co-routine or a future or an awaitable
    :param interval: the interval can also be given in the format of datetime.timedelta,
                      then seconds, minutes, hours, days, weeks parameters are ignored.
    :param loop: io loop if the provided job is a custom future linked up
                 with a different event loop.
    :return: schedule object, so it could be cancelled at will of the user by
             aschedule.cancel(schedule)
    """
    if loop is None:
        loop = asyncio.get_event_loop()
    start = loop.time()

    def wait_time_gen():
        count = 0
        while True:
            rand = random.randrange(round(interval.total_seconds()))
            tmp = round(start + interval.total_seconds() * count + rand - loop.time())
            yield tmp
            count += 1

    schedule = JobSchedule(job, wait_time_gen(), loop=loop)
    # add it to default_schedule_manager, so that user can aschedule.cancel it
    default_schedule_manager.add_schedule(schedule)
    return schedule
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号