热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python任务计划_Python:实现计划任务和打包程序

Python3.7.x有两种方式实现计划任务:scheduleAPscheduler打包python程序定时器:scheduleimportscheduleimpor

Python==3.7.x

有两种方式实现计划任务:

schedule

APscheduler

打包python程序

定时器:schedule

import schedule

import time,datetime

def task(name):

print("{0}".format(name))

# 每隔2秒执行一次任务

schedule.every(2).seconds.do(task, name)

# 每隔一小时执行一次任务

schedule.every().hour.do(task, name)

# 每天10:30执行一次任务

schedule.every().day.at("10:30").do(task, name)

while True:

schedule.run_pending()

# 因为schedule只是一个定时器,他不会死循环执行任务,所以我们这里需要使用while

time.sleep(1)

调度任务模块:apscheduler

# aps有两种方式写法

## 第一种(第一种写法)

from apscheduler.schedulers.blocking import BlockingScheduler

def task():

print('task')

aps = BlockingScheduler()

#在6月,7月,8月,11月和12月的第三个星期五的00:00,01:00,02:00和03:00执行job_function

aps.add_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')

aps.start()

## 第二种(第二种写法,在某些情况下可以避免报错,比如将python程序打包成执行文件后,第一种方法写的就会报错('LookupError: No trigger by the name "cron" was found').)

from apscheduler.schedulers.blocking import BlockingScheduler

from apscheduler.triggers.interval import IntervalTrigger

def task():

print('task')

# 每秒执行一次

trigger = IntervalTrigger(seconds=1)

aps = BlockingScheduler()

aps.add_job(func=a._start, trigger=trigger)

aps.start()

python程序打包命令

推荐一个制作icon的软件点击此处下载试用版

pip install PyInstaller==3.5

# 建议使用绝对路径

pyinstaller main_handler.py -F -p -i



推荐阅读
author-avatar
yangcong
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有