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

  

 

转载于:https://www.cnblogs.com/shione/p/10905987.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值