python的schedule定時模組

2021-09-02 18:41:55 字數 3064 閱讀 3568

import schedule

import time

from mysql_example import mysqlexample

import random

import datetime

import logging

import threading

logging.basicconfig(level=logging.notset)

#插入資料庫

definsertdate()

: number =

round

(random.uniform(0,

1000),

5)dtime = datetime.datetime.now(

).strftime(

"%y-%m-%d %h:%m:%s"

) insertsql =

""" insert into atest_doubledate (dtime,date_one) values (%s,%s)"""

x = mysqlexample(

) y = x.insert_date(dtime, number)

# 非同步呼叫insertdate

defrun_threaded

(func)

: s2 = threading.thread(target=func)

s2.start(

)#非同步呼叫run_threaded

defrun_thread()

: schedule.every(10)

.seconds.do(run_threaded, insertdate)

while

true

: schedule.run_pending(

) time.sleep(1)

#非同步呼叫計時

s1 = threading.thread(target=run_thread)

s1.start(

)

但是悲劇的發現了一些問題 兩次呼叫之間會有延時,這個延時還是會浮動的緩慢增加。10s種執行一次計算的話,大概不到乙個小時就會秒數+1,這個就很悲劇了。十分鐘差了0.28秒,這要是定時記錄資料就太悲劇了。

但是間隔時間比較長用這個還是很方便的,比如,每天執行一次,沒小時執行一次這樣的資料頻率用這個資料庫還是沒有問題的。

def

insertdate()

: dtime = datetime.datetime.now(

)print

(dtime)

number =

round

(random.uniform(0,

1000),

5)insertsql =

""" insert into atest_doubledate (dtime,date_one) values (%s,%s)"""

# delsql = "delete from atest_doubledate where id = %s"

# updatesql = "update atest_doubledate set dtime = %s where id = %s"

# res = mysql_alter_sql(insertsql, [(dtime, number), (dtime, number), (dtime, number)], manydate=true)

# res = mysql_alter_sql(delsql, 22)

# des = mysql_alter_sql(updatesql, [dtime, 23])

x = mysqlexample(

) y = x.insert_date(dtime.strftime(

"%y-%m-%d %h:%m:%s"

), number)

#info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:28:50, next run: 2018-12-06 16:29:00)

#2018-12-06 16:29:00.219063

#info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:29:00, next run: 2018-12-06 16:29:10)

#2018-12-06 16:29:10.220479

#info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:29:10, next run: 2018-12-06 16:29:20)

#2018-12-06 16:29:20.221768

info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:32:27, next run: 2018-12-06 16:32:37)

2018-12-06 16:32:37.927138

info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:32:37, next run: 2018-12-06 16:32:47)

2018-12-06 16:32:47.927408

info:schedule:running job every 10 seconds do tread_insertdate() (last run: 2018-12-06 16:32:47, next run: 2018-12-06 16:32:57)

2018-12-06 16:32:57.927678

還是建議吧延時和定時觸發的時間改為一致吧······,畢竟沒差多少。。。。。。

python 爬蟲定時 schedule

有時我們需要定時傳送郵件等操作,schedule可以實現定時功能 schedule 模組 引入schedule和time defjob print i m working.定義乙個叫job的函式,函式的功能是列印 i m working.schedule.every 10 minutes.do jo...

Python定時模組 schedule

pip install schedule 先安裝schedule模組schedule.every 5 minutes.do 函式名,引數 每隔五分鐘執行一次任務 schedule.every hour.do 函式名,引數 每隔一小時執行一次任務 schedule.every day.at 10 30...

python的schedule 模組用法示例

1 定時任務 usr bin env python coding utf 8 import time,os,sched 第乙個引數確定任務的時間,返回從某個特定的時間到現在經歷的秒數 第二個引數以某種人為的方式衡量時間 schedule sched.scheduler time.time,time....