简体   繁体   English

在Python 2.7中每n秒循环执行一次函数时的初始延迟

[英]Initial delay whilst looping a function every n seconds in Python 2.7

I would like to loop the function "logger" every second, ideally start and stop at certain times of the day too, however I haven't worked out the best way to do that yet. 我想每秒循环一次“记录器”功能,理想情况下也要在一天中的某些时间启动和停止,但是我还没有找到最佳的方法。 I've noticed in the log file that there's a delay between the first and second entry when printing the data. 我在日志文件中注意到,打印数据时,第一项和第二项之间存在延迟。 How do I solve this problem? 我该如何解决这个问题?

[u'(LON:SXX)'] - [u'14.38'] - [u'3.88M/6.72M'] - 12:12:36 [u'(LON:SXX)']-[u'14.38']-[u'3.88M / 6.72M']-12:12:36

[u'(LON:SXX)'] - [u'14.38'] - [u'3.88M/6.72M'] - 12:12:43 [u'(LON:SXX)']-[u'14.38']-[u'3.88M / 6.72M']-12:12:43

[u'(LON:SXX)'] - [u'14.38'] - [u'3.88M/6.72M'] - 12:12:44 [u'(LON:SXX)']-[u'14.38']-[u'3.88M / 6.72M']-12:12:44

[u'(LON:SXX)'] - [u'14.38'] - [u'3.88M/6.72M'] - 12:12:45 [u'(LON:SXX)']-[u'14.38']-[u'3.88M / 6.72M']-12:12:45

from selenium.webdriver.firefox import webdriver
from datetime import datetime
import time

starttime=time.time()

driver = webdriver.WebDriver()
driver.get('https://www.google.co.uk/finance?q=LON:SXX') 

def logger():
    with open("log.txt", "a") as f:
        # get the current price & time
        tme = datetime.now().strftime('%H:%M:%S')
        current_price = [price.text for price in driver.find_elements_by_xpath('//*[@id="price-panel"]/div[1]/span') if price.text]
        volume = [vol.text for vol in driver.find_elements_by_xpath('/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/table[1]/tbody/tr[4]/td[2]') if vol.text]
        symbol = [sym.text for sym in driver.find_elements_by_xpath('/html/body/div/div/div[3]/div[1]/div/div[2]/div[2]/span') if sym.text]
        f.write ("%s - %s - %s - %s \n" % (symbol, current_price, volume, tme))
        f.close

while True:
    logger()
    time.sleep(1.0 - ((time.time() - starttime) % 1.0))

You may choose one of this: - use your OS crontab, - use python crontab module - use a scheduler module 您可以选择以下选项之一:-使用操作系统crontab,-使用python crontab模块-使用调度程序模块

Please see bellow your code example modified to use the python apscheduler module: https://apscheduler.readthedocs.org/en/latest/ 请在下面看到您的代码示例已修改为使用python apscheduler模块: https ://apscheduler.readthedocs.org/en/latest/

from selenium.webdriver.firefox import webdriver
from datetime import datetime
import time

#to install 'apscheduler' use this command 'pip install apscheduler'
from apscheduler.scheduler import Scheduler

#init the scheduler
sched = Scheduler()


starttime=time.time()

driver = webdriver.WebDriver()
driver.get('https://www.google.co.uk/finance?q=LON:SXX') 

#here we are decor the logger function 

@sched.interval_schedule(seconds=1)
def logger():
    with open("log.txt", "a") as f:
        # get the current price & time
        tme = datetime.now().strftime('%H:%M:%S')
        current_price = [price.text for price in driver.find_elements_by_xpath('//*[@id="price-panel"]/div[1]/span') if price.text]
        volume = [vol.text for vol in driver.find_elements_by_xpath('/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/table[1]/tbody/tr[4]/td[2]') if vol.text]
        symbol = [sym.text for sym in driver.find_elements_by_xpath('/html/body/div/div/div[3]/div[1]/div/div[2]/div[2]/span') if sym.text]
        f.write ("%s - %s - %s - %s \n" % (symbol, current_price, volume, tme))
        f.close


sched.start()   

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

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