How do you handle a code where you have to pause the code at any given moment. For example, you could be reading files from server and server is going to be rebooted; you would want to pause the code so it stops trying to read the file from the server. You also wouldn't want to rerun the code if you have been running it for a long time. Is there a way to pause a code for certain amount of time in python?
I looked into this everywhere and couldn't find any solution. There were few solution that was mentioned.
raw_input("") :if you are reading millions of file,I don't think you would want to manually enter every time it reaches this part of code.
sleep(): you wouldn't know when to pause the code and for how long so I don't think this would work.
There is a anything I can implement to take care of this issue?
Thanks,
Shone
I understand you don't see any snippet of code and I am sorry for not having any code snippet. I have been thinking about this issue and trying to find a solution in case this issue were to arise in future.
答案 0 :(得分:1)
I don't know how this will affect efficiency-wise but can't you use sleep() inside a while loop or something like that. As in,
while not condition: sleep(100)
or just,
while not condition: pass
答案 1 :(得分:1)
我过去所做的是让我的脚本定期检查某个地方是否存在stop
文件,当你想暂停时可以手动将其放在那里。
因此,对于这个设计无限循环的例子,脚本会检查文件,如果存在,它会进入睡眠循环并继续每秒检查一次。当文件消失时,主循环将继续
import time, os.path
while True: # Main processing loop
while os.path.exists('path/to/file'):
time.sleep(1)
# Do processing stuff
# here
这是一个丑陋的黑客,但实施起来很简单。