{"id":7284,"date":"2024-04-09T11:01:01","date_gmt":"2024-04-09T03:01:01","guid":{"rendered":""},"modified":"2024-04-09T11:01:01","modified_gmt":"2024-04-09T03:01:01","slug":"python time.sleep\u7cbe\u786e\u5ef6\u65f6,Python time sleep()\u2013 time.sleep()\u65b9\u6cd5\u7684\u4e00\u7ad9\u5f0f\u89e3\u51b3\u65b9\u6848","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7284.html","title":{"rendered":"python time.sleep\u7cbe\u786e\u5ef6\u65f6,Python time sleep()\u2013 time.sleep()\u65b9\u6cd5\u7684\u4e00\u7ad9\u5f0f\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"
\n

\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u7a0b\u5e8f\u6216\u7a0b\u5e8f\u7684\u5404\u4e2a\u90e8\u5206\u5728\u77ed\u65f6\u95f4\u5185\u6267\u884c\u3002Python\u901a\u8fc7time.sleep()\u51fd\u6570\u4f7f\u6b64\u4efb\u52a1\u6beb\u4e0d\u8d39\u529b\u3002\u0164\u4ed6\u7684\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u8fd9\u4e2a\u529f\u80fd\u7684\u529f\u80fd\uff0c\u8fde\u540c\u5176\u5e94\u7528\u7a0b\u5e8f\u4e00\u8d77\u3002<\/p>\n

\u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5feb\u901f\u770b\u4e00\u4e0b\u672c\u6587\u6db5\u76d6\u7684\u4e3b\u9898\uff1a<\/p>\n

\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002:)<\/p>\n

\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528Python time.sleep()\uff1f<\/p>\n

\u7761\u7720\u529f\u80fd\u5728\u6211\u4eec\u8981\u505c\u6b62\u7a0b\u5e8f\u6d41\u5e76\u8ba9\u5176\u4ed6\u6267\u884c\u53d1\u751f\u7684\u60c5\u51b5\u4e0b\u8d77\u7740\u975e\u5e38\u91cd\u8981\u7684\u4f5c\u7528\u3002\u8be5\u51fd\u6570\u5728python\u7684\u4e24\u4e2a\u7248\u672c(\u53732\u548c3)\u4e2d\u90fd\u6709\u5b9a\u4e49\u3002\u5b83\u5c5e\u4e8ePython\u7684\u65f6\u95f4\u6a21\u5757\u3002\u57fa\u672c\u4e0a\uff0c\u8fd9\u4f1a\u589e\u52a0\u6267\u884c\u5ef6\u8fdf\uff0c\u5e76\u4e14\u53ea\u4f1a\u6682\u505c\u5f53\u524d\u7ebf\u7a0b\uff0c\u800c\u4e0d\u4f1a\u6682\u505c\u6574\u4e2a\u7a0b\u5e8f\u3002<\/p>\n

\u65f6\u95f4\u6a21\u5757<\/p>\n

Python\u7684time\u6a21\u5757\u4e2d\u63d0\u4f9b\u4e86Python time.sleep()\u51fd\u6570\u3002\u5728\u4f7f\u7528\u6b64Python\u51fd\u6570\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5bfc\u5165\u6b64\u6a21\u5757\uff1a<\/p>\n

import time<\/p>\n

\u5bfc\u5165\u6b64\u6a21\u5757\u540e\uff0c\u5373\u53ef\u4f7f\u7528time.sleep()\u51fd\u6570\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\u53e5\u6cd5\uff1a<\/p>\n

\u7761\u7720(\u79d2)<\/p>\n

\u5982\u60a8\u6240\u89c1\uff0c\u5b83\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u79d2\u3002\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u57fa\u672c\u4e0a\u4f1a\u5f15\u8d77\u8fd9\u4e48\u591a\u79d2\u7684\u5ef6\u8fdf\u3002\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662fvoid\u3002<\/p>\n

\u73b0\u5728\u8ba9\u6211\u4eec\u4e3e\u4e00\u4e9b\u4f8b\u5b50\u6765\u4e86\u89e3\u6b64\u529f\u80fd\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n

Python time.sleep()\u793a\u4f8b\uff1a<\/p>\n

\u8003\u8651\u4e0b\u9762\u7684\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u5728\u8f93\u51fa\u4e4b\u95f4\u5f15\u8d77\u4e00\u79d2\u7684\u5ef6\u8fdf\u3002<\/p>\n

\u4f8b\u5b50\uff1a<\/p>\n

import time # import time module<\/p>\n

sleep_time = 1 # time to add delay after first print statement<\/p>\n

print('Hello')<\/p>\n

time.sleep(sleep_time) # sleep time<\/p>\n

print('Edureka!')<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

\u5982\u679c\u6267\u884c\u4e86\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5219\u4f1a\u5728\u7a0b\u5e8f\u4e2d\u589e\u52a0\u5ef6\u8fdf\uff0c\u56e0\u6b64\uff0c\u4e0b\u4e00\u6761\u8bed\u53e5\u5c06\u57281\u79d2\u540e\u6267\u884c\u3002\u5bf9\u4e8e\u786e\u5207\u7684\u5ef6\u8fdf\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5c06\u6d6e\u70b9\u503c\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7ecf\u8fc7\u4e860.1\u79d2\uff0c\u5219\u5c06\u5ef6\u8fdf100\u6beb\u79d2\u3002<\/p>\n

\u8fd9\u662f\u53e6\u4e00\u4e2a\u793a\u4f8b\uff0c\u5b83\u5c06\u5728\u6267\u884c\u7a0b\u5e8f\u4e4b\u524d\u548c\u4e4b\u540e\u8fd4\u56de\u7cfb\u7edf\u65f6\u95f4\u3002<\/p>\n

\u4f8b\u5b50\uff1a<\/p>\n

# sleep demonstration<\/p>\n

import time<\/p>\n

# Start time<\/p>\n

print(\"The time of code execution begin is : \", end =\"\")<\/p>\n

print(time.ctime())<\/p>\n

# haulting program<\/p>\n

time.sleep(6)<\/p>\n

# end time<\/p>\n

print(\"The time of code execution end is : \", end =\"\")<\/p>\n

print(time.ctime())<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

\u5f00\u59cb\u6267\u884c\u4ee3\u7801\u7684\u65f6\u95f4\u662f\uff1aSun Jun 23 22:36:19 2019\u5f00\u59cb\u6267\u884c\u4ee3\u7801\u7684\u65f6\u95f4\u662f\uff1aSun Jun 23 22:36:25 2019\u8fdb\u7a0b\u8fd4\u56de0(0x0)\u6267\u884c\u65f6\u95f4\uff1a6.089 s\u6309\u4efb\u610f\u952e\u7ee7\u7eed\u3002\u3002\u3002<\/p>\n

\u7761\u7720\u793a\u4f8b\uff1a<\/p>\n

\u4ee5\u4e0b\u662f\u7761\u7720\u529f\u80fd\u7684\u793a\u4f8b\uff1a<\/p>\n

import time<\/p>\n

startTime = time.time()<\/p>\n

for i in range(5, 10):<\/p>\n

print(i)<\/p>\n

# making delay for 1 second<\/p>\n

time.sleep(1)<\/p>\n

endTime = time.time()<\/p>\n

elapsedTime = endTime - startTime<\/p>\n

print(\"Elapsed Time = %s\" % elapsedTime)<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

56789<\/p>\n

\u7ecf\u8fc7\u7684\u65f6\u95f4= 5.006335258483887\u8fdb\u7a0b\u8fd4\u56de0(0x0)\u6267\u884c\u65f6\u95f4\uff1a5.147 s<\/p>\n

\u6bcf\u6b21\u505c\u6b62\u6267\u884c1\u79d2\u949f\uff0c\u56e0\u6b64\u5b8c\u6574\u6267\u884c\u8017\u65f65\u79d2\u3002\u53e6\u5916\uff0c\u6267\u884c\u6240\u9700\u7684\u989d\u5916\u65f6\u95f4\u662f\u7cfb\u7edf\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u540e\u53f0\u64cd\u4f5c\u7684\u65f6\u95f4\u3002<\/p>\n

python sleep()\u7684\u4e0d\u540c\u5ef6\u8fdf\u65f6\u95f4<\/p>\n

\u6839\u636e\u6240\u9700\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7684\u7a0b\u5e8f\u6267\u884c\u4e4b\u95f4\u6dfb\u52a0\u4e0d\u540c\u7684\u5ef6\u8fdf\u65f6\u95f4\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n

\u4f8b\u5b50\uff1a<\/p>\n

import time<\/p>\n

for i in [1, 0.1, 2, 0.3]:<\/p>\n

print(\"I will sleep for %s\" % i, end='')<\/p>\n

print(\" seconds\")<\/p>\n

time.sleep(i)<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

\u6211\u8981\u77611\u79d2<\/p>\n

\u6211\u8981\u77610.1\u79d2<\/p>\n

\u6211\u8981\u77612\u79d2<\/p>\n

\u6211\u8981\u77610.3\u79d2<\/p>\n

\u8fdb\u7a0b\u8fd4\u56de0(0x0)\u6267\u884c\u65f6\u95f4\uff1a3.538 s<\/p>\n

\u60f0\u6027\u6253\u5370\uff1a<\/p>\n

\u5982\u679c\u8981\u4ee5\u7cbe\u7f8e\u7684\u65b9\u5f0f\u6253\u5370\u67d0\u4e9b\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bsleep()\u51fd\u6570\u8fdb\u884c\u6253\u5370\uff1a<\/p>\n

# importing time module<\/p>\n

import time<\/p>\n

message = \"Some fancy character printing!\"<\/p>\n

for i in message:<\/p>\n

print(i)<\/p>\n

time.sleep(0.3)<\/p>\n

\u5982\u679c\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u60a8\u4f1a\u53d1\u73b0\u6bcf\u4e2a\u5b57\u7b26\u7684\u6253\u5370\u5ef6\u8fdf\uff0c\u770b\u8d77\u6765\u5f88\u82b1\u54e8\u3002<\/p>\n

Python\u7ebf\u7a0b\u7761\u7720<\/p>\n

\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0csleep()\u88ab\u8bc1\u660e\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u5728\u6267\u884c\u65f6\u4f1a\u5728\u6b63\u5728\u6267\u884c\u7684\u5f53\u524d\u7ebf\u7a0b\u4e2d\u589e\u52a0\u5ef6\u8fdf\u3002<\/p>\n

\u4f8b\u5b50\uff1a<\/p>\n

import time<\/p>\n

from threading import Thread<\/p>\n

class Runner(Thread):<\/p>\n

def run(self):<\/p>\n

for x in range(0, 7):<\/p>\n

print(x)<\/p>\n

time.sleep(2)<\/p>\n

class Delay(Thread):<\/p>\n

def run(self):<\/p>\n

for x in range(106, 109):<\/p>\n

print(x)<\/p>\n

time.sleep(7)<\/p>\n

print(\"Staring Runner Thread\")<\/p>\n

Runner().start()<\/p>\n

print(\"Starting Delay Thread\")<\/p>\n

Delay().start()<\/p>\n

print(\"Done\")<\/p>\n

\u4ee5\u4e0b\u662f\u4e0a\u9762\u7684\u7ebf\u7a0b\u793a\u4f8b\u7684\u8f93\u51fa\uff1a<\/p>\n

\"python<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

\u5982\u679c\u6267\u884c\u8be5\u7a0b\u5e8f\uff0c\u60a8\u4f1a\u6ce8\u610f\u5230\u6574\u4e2a\u7a0b\u5e8f\u5e76\u6ca1\u6709\u505c\u6b62\uff0c\u800c\u662f\u53ea\u6709\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u7ebf\u7a0b\u624d\u7ee7\u7eed\u8fd0\u884c\uff0c\u7136\u540e\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n

\u5e94\u7528 \uff1a<\/p>\n

\u6b64\u65b9\u6cd5\u6709\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u6f02\u4eae\u7684\u7528\u6237\u754c\u9762\uff0c\u4ee5\u67d0\u79cd\u7cbe\u7f8e\u7684\u65b9\u5f0f\u6253\u5370\u83dc\u5355\u6216\u6807\u9898\uff0c\u4f46\u662f\uff0c\u91cd\u8981\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u4e00\u662f\u6682\u505c\u8981\u6267\u884c\u7684\u540e\u53f0\u8fdb\u7a0b\u5728\u4e00\u5b9a\u95f4\u9694\u5185\u3002<\/p>\n

\u5e94\u7528\u5b9e\u4f8b\uff1a<\/p>\n

import time<\/p>\n

string = \"Edureka!\"<\/p>\n

print_string = \"\"<\/p>\n

for i in range(0, len(string)):<\/p>\n

print_string = print_string + string[i]<\/p>\n

print(print_string)<\/p>\n

time.sleep(2)<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

E<\/p>\n

Ed<\/p>\n

Edu<\/p>\n

Edur<\/p>\n

Edure<\/p>\n

Edurek<\/p>\n

Edureka<\/p>\n

Edureka\uff01<\/p>\n

\u6b63\u5982\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u7684\u90a3\u6837\uff0csleep\u51fd\u6570\u4f1a\u4f7f\u7a0b\u5e8f\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u800cPython\u7684\u65f6\u95f4\u6a21\u5757\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u5982\u4f55\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\u5e76\u52a8\u6001\u4f7f\u7528\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n

\u52a8\u6001\u7761\u7720\u793a\u4f8b<\/p>\n

\u8fd9\u662f\u4e00\u4e2a\u7761\u7720\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u5229\u7528\u6765\u81ea\u7528\u6237\u7684\u8f93\u5165\u5728\u4e24\u4e2a\u6253\u5370\u529f\u80fd\u4e4b\u95f4\u6dfb\u52a0\u5ef6\u8fdf\uff0c\u5e76\u6253\u5370\u6267\u884c\u6253\u5370\u529f\u80fd\u6240\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u4ee5\u4e0b\u793a\u4f8b\u57fa\u4e8ePython3.x\u3002<\/p>\n

import time<\/p>\n

def sleeper():<\/p>\n

while True:<\/p>\n

num = input('Enter wait time: ')<\/p>\n

try:<\/p>\n

num = float(num)<\/p>\n

except ValueError:<\/p>\n

print('Number only.n')<\/p>\n

continue<\/p>\n

# Run our time.sleep() command,<\/p>\n

# and show the before and after time<\/p>\n

print('Before: %s' % time.ctime())<\/p>\n

time.sleep(num)<\/p>\n

print('After: %sn' % time.ctime())<\/p>\n

try:<\/p>\n

sleeper()<\/p>\n

except KeyboardInterrupt:<\/p>\n

print('nnException Exiting.')<\/p>\n

exit()<\/p>\n

\u8f93\u51fa\uff1a<\/p>\n

\u8f93\u5165\u7b49\u5f85\u65f6\u95f4\uff1a1\u4e4b\u524d\uff1aSun Jun 23 22:44:13 2019\u4e4b\u540e\uff1aSun Jun 23 22:44:14 2019\u8f93\u5165\u7b49\u5f85\u65f6\u95f4\uff1a3\u4e4b\u524d\uff1aSun Jun 23 22:44:16 2019\u4e4b\u540e\uff1aSun Jun 23 22\uff1a 2019\u5e7444\u670819\u65e5<\/p>\n

\u51c6\u786e\u6027<\/p>\n

\u5982\u679c\u8981\u5728\u66f4\u77ed\u7684\u65f6\u95f4\u5185\u505c\u6b62\u6267\u884c\uff0c\u5219\u6b64\u51fd\u6570\u53d7\u64cd\u4f5c\u7cfb\u7edf\u7684\u9650\u5236\uff0c\u56e0\u4e3a\u6b64\u51fd\u6570\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edfsleep()\u51fd\u6570\uff0c\u56e0\u6b64\u5728Linux\u4e2d\uff0c\u7b49\u5f85\u65f6\u95f4\u53ef\u4ee5\u6bd4Windows\u77ed\u3002<\/p>\n

\u6982\u62ec<\/p>\n

\u5728\u4e0a\u9762\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86python\u4e2d\u7684sleep()\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u589e\u52a0\u7a0b\u5e8f\u6267\u884c\u7684\u5ef6\u8fdf\uff0c\u6b64\u7a0b\u5e8f\u5305\u4f4d\u4e8ePython\u7684\u65f6\u95f4\u6a21\u5757\u4e2d\uff0c\u8be5\u6a21\u5757\u57fa\u672c\u4e0a\u4f7f\u7528\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u7684sleep()\u51fd\u6570\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u6709\u5173\u5982\u4f55\u4f7f\u7528\u6b64\u529f\u80fd\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5e76\u4ecb\u7ecd\u4e86\u7761\u7720\u5e94\u7528\u7a0b\u5e8f\u3002\u6f14\u793a\u4e86\u4f7f\u7528\u8be5\u529f\u80fd\u7684\u7406\u60f3\u65b9\u5f0f\u4ee5\u53ca\u5b83\u5728\u7ebf\u7a0b\u73af\u5883\u4e2d\u7684\u5de5\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"python time.sleep\u7cbe\u786e\u5ef6\u65f6,Python time sleep()\u2013 time.sleep()\u65b9\u6cd5\u7684\u4e00\u7ad9\u5f0f\u89e3\u51b3\u65b9\u6848\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u7a0b\u5e8f\u6216\u7a0b\u5e8f\u7684\u5404\u4e2a\u90e8\u5206\u5728\u77ed...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7284"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7284"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7284\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}