简体   繁体   English

使用python睡眠模块时发生了什么?

[英]What is happening when you use the python sleep module?

What exactly is happening when I call time.sleep(5) in a python script? 当我在python脚本中调用time.sleep(5)时到底发生了什么? Is the program using a lot of resources from the computer? 该程序是否使用了来自计算机的大量资源?

I see people using the sleep function in their programs to schedule tasks, but this requires you leave your hard drive running the whole time right? 我看到人们在他们的程序中使用sleep功能来安排任务,但是这需要你让硬盘一直运行吗? That would be taking for you computer over the long haul right? 从长远来看,那将是你的电脑吗?

I'm trying to figure out what's to run programs at specific times remotely, but I haven't found an explanation of how to do this that is very intuitive. 我试图找出在特定时间远程运行程序的内容,但我还没有找到如何做到这一点非常直观的解释。

Any suggestions? 有什么建议?

sleep will mark the process (thread) for being inactive until the given time is up. sleep会将进程(线程)标记为处于非活动状态,直到给定时间结束。 During this time the kernel will simply not schedule this process (thread). 在此期间,内核将不会安排此过程(线程)。 It will not waste resources. 不会浪费资源。

Hard disks typically have spin-down policies based solely on their usage. 硬盘通常具有仅基于其使用的降速策略。 If they aren't accessed for a specific time, they will spin down. 如果在特定时间内没有访问它们,它们将会降速。 They will spin up as soon as some process (thread) is accessing them again. 一旦某个进程(线程)再次访问它们,它们就会启动。

This means that letting a process (thread) sleep for some time gives the hard disk a chance to spin down (especially if the sleep duration is large, say, more than some minutes). 这意味着让进程(线程)休眠一段时间会让硬盘有机会降速(特别是如果睡眠持续时间很长,比如超过几分钟)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM