我有一个数据集命名当前正在运行的作业。我想监视它们,看它们是否比正常运行的时间更长,由阈值定义。
最后发送所有标记作业的所有消息
flaged_jobs = []; for job in job_bank: if job.average_time < 5: if (current_time - start_time) == 4 * (current_time - start_time): flaged_jobs.append(job) elif job.average_time >= 5 and job.average_time < 20 : if (current_time - start_time) == 2 * (current_time - start_time): flaged_jobs.append(job) elif job.average_time >= 5 and job.average_time < 20 : if (current_time - start_time) == 1.5 * (current_time - start_time): flaged_jobs.append(job) else: if (current_time - start_time) == .5 * (current_time - start_time): flaged_jobs.append(job) for flaged in flaged_jobs: send_message(flaged)
在性能方面,处理此问题的最佳方法是什么?我的伪代码是处理这个问题的好方法吗?应该包含一个字典而不是列表?工作可以从一次几百到几万。我限制在2.6x python。我该怎么命名这个剧本,也许是runaway_jobs.py?
答案 0 :(得分:0)
我不确定最好的方法!
但我想我可以修复你的代码!
flaged_jobs = [];
for job in job_bank:
# first of all. get start_time from job status
job_start_time = get_start_time(job)
elapsed_time = current_time - job_start_time
# Fix your logic, < 5 <=, < 20 <=...
if job.average_time < 5:
# (current_time - start_time) == 4 * (current_time - start_time)
# is always False except current_time == start_time, so I fixed it.
if elapsed_time >= 4 * job.average_time:
flaged_jobs.append(job)
elif 5 <= job.average_time < 20:
if elapsed_time == 2 * job.average_time:
flaged_jobs.append(job)
elif 20 <= job.average_time < 3 * 60:
if elapsed_time == 1.5 * job.average_time:
flaged_jobs.append(job)
elif 3 * 60 <= job.average_time:
if elapsed_time == .5 * job.average_time:
flaged_jobs.append(job)
else:
continue
for flaged in flaged_jobs:
send_message(flaged)