每分钟运行Python函数一分钟

时间:2013-07-09 18:33:14

标签: python mysql function loops timer

我最近开始学习Python,我有一个应用程序,我想创建一个文本文件,每两秒钟记录一次数据,然后再用新文件重新开始。我写的是为打开的文件格式化为SQL并且正在添加SQL INSERT查询。

我遇到的麻烦是创建实际正常工作的代码。 Python本身只能在已经打开的文件中插入新的SQL行,但输出如下所示:

[blank line is here because of the \n]
INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');
INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');

我的问题是我需要获取数据,因此顶部没有新行,最后一行没有分号。

这是我的来源:

from threading import Timer
import time

def start():
    t = Timer(1.0,start)

    foo = "7113"
    bar = "1337"
    date = time.strftime('%Y-%m-%d %H:%M:%S')
    sql = "\nINSERT INTO table (column 1, column 2) VALUES ('%s', '%s');" % (foo, bar)

    query = open(date + ".sql", "a")
    query.write(sql)
    query.close()
    t.start()

start()

由于我不是最好的语言,我想要做的更简单的版本是:

  1. 让一个函数运行第一个代码,吐出INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');而不用前面的额外行但用分号。
  2. 让另一个函数运行58次,在SQL查询之前添加\n行,之后添加分号。
  3. 让最后一个函数运行第60次,包括\n但没有分号。
  4. 关闭当前文件并生成一个新文本文件,其中包含该分钟的时间戳,例如2013-07-09 12-30-00.sql,然后重复2013-07-09 12-31-00.sql的过程,依此类推。
  5. 我想在那之后还有很多东西,比如获取PHP或Python脚本来读取日期并执行它,或者我可以让它们全部执行,然后按升序排序表。但是,这可以在以后解决和研究。我只是想让这个工作,因为我现在已经搜索了几个小时如何做到这一点,我找不到任何有用的东西。不过,我确信答案是显而易见的。

    感谢阅读,希望你们能帮忙!我已经使用这个网站一堆现在有用的花絮,这太棒了。想出这个的人需要得到一些赞美。

    此外,如果有人有任何有用的资源来学习Python(我通过Codecademy学习),我会非常感激他们。这比学习C ++更有趣。

1 个答案:

答案 0 :(得分:0)

我通过使用global counting变量并使用ifelif语句来解决问题。目前,该脚本每500毫秒运行一次没有问题。如果有人想看我的代码,那么它就是:

Commented version because syntax highlighting here with comments is screwing up for whatever reason.

from threading import Timer
import xml.etree.ElementTree as ET
import urllib
import time

datetime = time.strftime('%Y-%m-%d %H.%M')
filename = datetime + ".sql"
url = "localhost/send.cgi"
interval = 0.5
table_name = "0001"
columns = "(time, foo, bar)"
counting = 0 

def parser():
    t = Timer(interval, parser)
    global counting
    global filename
    query = open(filename, "a")

    if counting == 0:
        date = int(time.time()*1000)
        web = urllib.urlopen(url)
        tree = ET.parse(web)
        root = tree.getroot()
        foo = root[1].text.replace(" ", "")
        bar = root[2].text.replace(" ", "")
        sql = "INSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
        counting += 1
        query.write(sql)
        print counting
        t.start()
    elif (counting > 0) and (counting < 59):
        date = int(time.time()*1000)
        web = urllib.urlopen(url)
        tree = ET.parse(web)
        root = tree.getroot()
        foo = root[1].text.replace(" ", "")
        bar = root[2].text.replace(" ", "")
        sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
        counting += 1
        query.write(sql)
        print counting
        t.start()
    elif counting == 59:
        date = int(time.time()*1000)
        web = urllib.urlopen(url)
        tree = ET.parse(web)
        root = tree.getroot()
        foo = root[1].text.replace(" ", "")
        bar = root[2].text.replace(" ", "")
        sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s')" % (table_name, columns, date, foo, bar)
        counting += 1
        query.write(sql)
        print counting
        t.start()
    elif counting == 60:
        query.close()
        datetime = time.strftime('%Y-%m-%d %H-%M')
        filename = datetime + ".sql"
        counting = 0
        t.start()
parser()

如果可以进行任何改进,我很乐意看到它们! &lt; 3此代码适用于某种类型的Web服务器。它可能需要一些修改才能工作。 Web服务器所做的是,在请求时,发送一个虚拟XML文件(它只是数据,而不是文件),这个代码请求它,解释它,并将其发送到.sql文件,以便我以后可以将它们执行到表到某事。最后,代码将抓取十个不同的数据列,并创建一个小时的文件,即7200半秒。

享受!

编辑:是的,显然SQL语法不起作用。故障排除。