time.sleep

有时候我们需要模拟程序的暂停,或者耗时。就需要让程序睡一会儿。

简介

Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。

语法

time.sleep(t)

例子

#!/usr/bin/python
import time
 
print "Start : %s" % time.ctime()
time.sleep( 5 )
print "End : %s" % time.ctime()

个人使用案例

import scrapy
import os
import time

class CurlSpider(scrapy.Spider):
    name = 'curlspider'
    start_urls = ['https://blog.csdn.net/ryo1060732496']

    # init curls
    urls = []
    path='data/blog.txt'
    # 指定以 utf-8 的格式读取文件
    with open(path, encoding='utf-8') as f:
        for line in f:
            urls.append(line)

    def parse(self, response):
        for url in self.urls:
            cmd = "curl " + str(url)
            print(cmd)
            os.system(cmd)
            
            ## sleep
            time.sleep(5)

参考资料

Python time sleep()方法