thinkphp5+python.apscheduler实现计划任务
1.thinkphp5配置自定义命令行
/application/console/command
namespace app\console\command; use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\autotask\task; class Test extends Command{ /**
* 定义命令
* 命令名称是 test
*/
protected function configure()
{
$this->setName('test')->setDescription('Here is the market test task ');
}
/**
* 命令执行的内容
* @param Input $input
* @param Output $output
*/
protected function execute(Input $input, Output $output)
{
$output->writeln("StartCommand Test:");
$object = new \app\autotask\task\Test();
$object->run();
$output->writeln("End Test..");
}
}
2.python安装APScheduler是Python的一个定时任务框架,可以很方便的满足用户定时执行或者周期执行任务的需求,它提供了基于日期date、固定时间间隔interval 、以及类似于Linux上的定时任务crontab类型的定时任务。并且该框架不仅可以添加、删除定时任务,还可以将任务存储到数据库中,实现任务的持久化,所以使用起来非常方便。
更多了解 https://pypi.org/project/APScheduler/
pip install apscheduler
#!/user/bin/env python
#
# -*- coding: UTF-8 -*- import time
from apscheduler.schedulers.blocking import BlockingScheduler
import logging
import os logger = logging.getLogger()
logger.setLevel(logging.DEBUG) fileHandler = logging.FileHandler('./log/task.log', mode='w', encoding='UTF-8')
fileHandler.setLevel(logging.NOTSET) formatter = logging.Formatter('%(asctime)s - %(message)s')
fileHandler.setFormatter(formatter) logger.addHandler(fileHandler) def task():
output = os.popen('php think test')
logger.info(output)
print output.read() if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(task, 'cron', hour='9-22', second = '*/1')
#scheduler.add_job(task, 'cron', hour='9-22', second = '*/1')
scheduler.add_job(task, 'cron', hour='9-22', second = '*/1')
scheduler.add_job(task, 'cron', hour = 22,minute = 1)
scheduler.add_job(task, 'interval', minutes=1)
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
thinkphp5+python.apscheduler实现计划任务的更多相关文章
- Python核心团队计划2020年停止支持Python2,NumPy宣布停止支持计划表
Python核心团队计划在2020年停止支持Python 2.NumPy项目自2010年以来一直支持Python 2和Python 3,并且发现支持Python 2对我们有限的资源增加了负担:因此,我 ...
- python APScheduler
简介 APScheduler基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务 ...
- python APScheduler模块
简介 一般来说Celery是python可以执行定时任务, 但是不支持动态添加定时任务 (Django有插件可以动态添加), 而且对于不需要Celery的项目, 就会让项目变得过重. APSchedu ...
- python性能测试大致计划
hi guy: 如果注意到创建时间,那就对了.这份文章,是我学习Python一个月以后动手写的. 写下这份计划以后,只完成了第一步,其中磕磕绊绊编写代码的过程,很大一部分时间是完全用txt写的 ...
- python apscheduler的使用
from apscheduler.schedulers.blocking import BlockingSchedulerfrom datetime import datetime def my_jo ...
- windows开机自启python服务(任务计划程序+bat脚本)
需求:根据上海某银行 的需求,使用到获取数据服务的软件 只能在windows上运行,所以有 windows系统开机用户登录后自启动python flask服务 的需求: 相关工具:win10系统中,使 ...
- Python - 时间相关与计划任务
Python - 时间处理与定时任务 1.计算明天和昨天的日期 # 获取今天.昨天和明天的日期 # 引入datetime模块 import datetime #计算今天的时间 today = date ...
- 【head first python】学习计划
1 初识Python:人人都爱列表 2 共享你的代码:函数模块 3 文件与异常:处理错误 4 持久存储:数据保存到文件 5 推导数据:处理数据! 6 定制数据对象:打包代码与数据 7 Web开发:集成 ...
- Python百题计划
一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...
随机推荐
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- Elasticsearch 如何使用RESTful API
所有其他语言可以使用 RESTful API 通过端口 9200 和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch .事实上,正如你所看到的 ...
- Python学习笔记006
算术运算符 加+ 减- 乘* 除/ 整除//,地板除 取余% 指数** ()区分 优先级 比较运算符 赋值 = 等于 == 不等于 != 大于等于 >= 小于等于 <=
- day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...
- 安装本地jar到maven仓库
mvn install:install-file -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar -D ...
- Debug运行项目时报错,connected to the target VM, address: '127.0.0.1:50495', transport: 'socket'
Debug运行项目时报错,无法进入Debug,猜想出错原因可能是未正确关闭IDEA. 解决方法,先直接运行项目,然后停掉项目,再用Debug模式启动,问题解决.
- leetcode295 Find Median from Data Stream
""" Median is the middle value in an ordered integer list. If the size of the list is ...
- 别再写getter,setter方法了,用Lombok来简化你的代码吧
前言 在实际开发中,有些代码是重复的.IDE一键生成的,不写不行,写了又觉得代码太臃肿,不美观.如果你也有这种体会,那么,请使用Lombok插件吧,真的非常好用.Lombok的使用也非常简单,都是各种 ...
- pytorc人工神经网络Logistic regression与全连接层
//2019.10.08神经网络与全连接层1.logistics regression逻辑回归的思想是将数据利用激活函数sigmoid函数转换为0-1的概率,然后定义一定的阈值0.5,大于阈值则为一类 ...
- ZCGL大数据平台性能优化
对HBase数据库建索引 参见:基于Phoenix对HBase建索引 https://www.cnblogs.com/ratels/p/11203313.html 将HBase访问微服务整合到模块 ...