Home  >  Article  >  Backend Development  >  What is the Python command line scheduled task automation workflow?

What is the Python command line scheduled task automation workflow?

王林
王林forward
2023-05-13 17:01:111635browse

1. Usage scenarios

Execute the jmeter script regularly and execute command line commands at intervals through the python timer.

2. Library

os, datetime, threading

(1) Use threading.Timer() timer to implement scheduled tasks

Timer method Description
Timer(interval, function, args=None, kwargs=None) Create timer
cancel() Cancel timer
start() Use thread mode to execute
join(self, timeout=None) Wait for the end of thread execution

timer The most basic understanding is that timers can start multiple scheduled tasks. These timer tasks are executed asynchronously, so there is no problem of waiting for sequential execution.

3. Run the script

jmeter execution command line

jmeter -n -t script name.jmx -l script report name.jtl

Parameter description:

  • n Non-GUI mode, command line mode (indicates running in non-GUI mode)

  • -t Test file, jmeter test script file to be run (generally use absolute path)

  • -l Result file, file to record the results

  • - h Get jmeter help information

  • -r Remote execution, start the remote server (start all remote agents configured by remote-hosts in non-gui mode)

  • -R Remote execution, (start the specified machine (IP: PORT) as a proxy machine in non-gui mode)

  • -e Generate test report after setting up the test

  • -o Specify the folder where the test report is generated. The folder must be empty/does not exist

  • -H Agent host (set the one used by jmeter Proxy host)

  • -P Proxy port (set the proxy port used by jmeter)

  • -X Exit (the test ends in non-gui mode Exit at time)

4. Script

import os
from datetime import datetime
from threading import Timer
# 定时任务
def task():
    now = datetime.now()
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    print(datetime.now())
    a = os.system("jmeter -n -t /Users/liyinchi/workspace/功能测试/好慷/测试数据(压测脚本)/阶梯拼团多维表格20230418.jmx -l /Users/liyinchi/workspace/功能测试/好慷/测试数据(压测脚本)/阶梯拼团多维表格20230418-result.jtl")
    print(a)
# 执行器
def func():
    task()
    t = Timer(60*1, func)
    t.start()
func()

Execution result:

What is the Python command line scheduled task automation workflow?

5. Commonly used scheduled tasks in python

while True: sleep()

threading.Timer timer

Timeloop library performs scheduled tasks

Scheduling module sched

Scheduling Module schedule

Task framework APScheduler

Distributed message system celery executes scheduled tasks

Use the scheduled tasks that come with windows

6. Four methods are used Python calls the command line

(1) os.system

import os
a=os.system("ls")
a

The output will be displayed when running the program, and the return value a is the program exit code

(2) os.popen

import os
a=os.popen("ls")
a.readline()

The return value is a file file,

file.readlines() is the return value of the command

(3) subprocess

can be created in the python program Subprocess,

subprocess.call()

import subprocess
 subprocess.call(['ls','-l' ])

Among them, 'ls’ corresponds to the command entered on the command line, and -l is the corresponding operation. Returns the program exit code, similar to os.system

subprocess.check_output('ls')

Returns standard output, similar to os.popen.

You can also call the Popen object to perform operations. subprocess

import subprocess
child = subprocess.Popen('ping -c4 blog.linuxeye.com',shell=True)

At this time, multiple commands can be used to control the subprocess. You can also use subprocess.PIPE to connect the input and output of the self-process...

(4) commands

import commands
 commands.getoutput('ls')

Return to program output

The above is the detailed content of What is the Python command line scheduled task automation workflow?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete