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

时间:2021-09-09 04:22:25

What exactly is happening when I call time.sleep(5) in a python script? Is the program using a lot of resources from the computer?

当我在python脚本中调用time.sleep(5)时到底发生了什么?该程序是否使用了来自计算机的大量资源?

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? 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?

1 个解决方案

#1


3  

sleep will mark the process (thread) for being inactive until the given time is up. During this time the kernel will simply not schedule this process (thread). It will not waste resources.

sleep会将进程(线程)标记为处于非活动状态,直到给定时间结束。在此期间,内核将不会安排此过程(线程)。它不会浪费资源。

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).

这意味着让进程(线程)休眠一段时间会使硬盘有机会降速(特别是如果睡眠持续时间很长,比如超过几分钟)。

#1


3  

sleep will mark the process (thread) for being inactive until the given time is up. During this time the kernel will simply not schedule this process (thread). It will not waste resources.

sleep会将进程(线程)标记为处于非活动状态,直到给定时间结束。在此期间,内核将不会安排此过程(线程)。它不会浪费资源。

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).

这意味着让进程(线程)休眠一段时间会使硬盘有机会降速(特别是如果睡眠持续时间很长,比如超过几分钟)。