回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

Nuro ML Infrastructure Phone Screen

🔗
匿名用户-XBMVL  2024-1-13 05:25:04 来自APP |倒序浏览

2024(1-3月) 码农类General 博士 全职@nuro - 网上海投 - 技术电面  | 😐 Neutral 😣 Hard | Other | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
implement a task scheduler function in single process
1. adhoc job: client submit a callable (something can be run) request, scheduler executed once
2. periodic job: client register a callable with a period. scheduler will then take over and run the callable every x seconds

答案:

import threading
import time
from datetime import datetime, timedelta
from functools import partial

class Task:
    def __init__(self, action, period=None, execute_once=False):
        self.action = actio
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
=True)
            self.tasks.append(task)

# Example Usage
def my_task():
    print(f"Task executed at {datetime.now()}")

scheduler = TaskScheduler()

# Adding an ad-hoc task
scheduler.add_task(my_task)

# Adding a periodic task
scheduler.add_task(partial(my_task), period=5)

评分

参与人数 1大米 +10 收起 理由
匿名用户-SYICK + 10 欢迎分享你知道的情况,会给更多大米奖励!

查看全部评分


上一篇:PIMCO -quant 求Hirevue面经
下一篇:英伟达 Software QA - Deep Learning Intern
🔗
ken922995 2024-1-17 07:09:30 | 只看该作者
全局:
我是VO system design關遇到這題 同職缺
回复

使用道具 举报

🔗
laobai2024 2024-4-19 15:23:47 | 只看该作者
全局:
可以用heap 外加condition variable
  1. import threading
  2. import heapq
  3. from datetime import datetime, timedelta

  4. class Task:
  5.     def __init__(self, func, interval=None):
  6.         self.func = func
  7.         self.interval = interval
  8.         self.next_run_time = None

  9.     def schedule_next_run(self, from_time=None):
  10.         if from_time is None:
  11.             from_time = datetime.now()
  12.         self.next_run_time = from_time + timedelta(seconds=self.interval)

  13.     def run(self):
  14.         self.func()
  15.         if self.interval is not None:
  16.             self.schedule_next_run()

  17. class Scheduler:
  18.     def __init__(self):
  19.         self.lock = threading.Lock()
  20.         self.condition = threading.Condition(self.lock)
  21.         self.tasks = []
  22.         self.running = True
  23.         threading.Thread(target=self._execute_tasks).start()

  24.     def _execute_tasks(self):
  25.         with self.condition:
  26.             while self.running:
  27.                 while self.tasks and self.running:
  28.                     now = datetime.now()
  29.                     next_run_time, task = self.tasks[0]
  30.                     if next_run_time > now:
  31.                         # Wait until the next task is ready or a new task is scheduled
  32.                         timeout = (next_run_time - now).total_seconds()
  33.                         self.condition.wait(timeout)
  34.                     else:
  35.                         # Remove the task and run it outside the lock to prevent blocking other operations
  36.                         heapq.heappop(self.tasks)
  37.                         threading.Thread(target=task.run).start()

  38.     def stop(self):
  39.         with self.condition:
  40.             self.running = False
  41.             self.condition.notify_all()

  42.     def schedule_task(self, task, delay=0):
  43.         run_time = datetime.now() + timedelta(seconds=delay)
  44.         with self.condition:
  45.             if task.interval is not None and task.next_run_time is None:
  46.                 task.schedule_next_run(run_time)
  47.             else:
  48.                 task.next_run_time = run_time
  49.             heapq.heappush(self.tasks, (task.next_run_time, task))
  50.             # If the new task is sooner than the current earliest task, notify the condition to re-evaluate waiting
  51.             if self.tasks[0][0] == task.next_run_time:
  52.                 self.condition.notify()

  53. # Usage
  54. def my_task():
  55.     print("Task executed at", datetime.now())

  56. def periodic_task():
  57.     print("Periodic task executed at", datetime.now())

  58. scheduler = Scheduler()
  59. scheduler.schedule_task(Task(my_task), delay=5)
  60. scheduler.schedule_task(Task(periodic_task, interval=10))
复制代码

评分

参与人数 1大米 +2 收起 理由
羊驼宝宝 + 2 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

🔗
BIRCHSS 2024-9-28 16:35:48 | 只看该作者
全局:
我朋友是  Nuro 这个职位的 hiring manager,目前还在招,华裔但是不咋会中文,托我帮他在华裔社区发帖。 要是有想去的朋友直接V 我: usmydream   招聘链接https://www.nuro.ai/careersitem?gh_jid=6202116
回复

使用道具 举报

全局:
这是第一轮店面还是第二轮
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表