python timed task execution command line

1. Usage scenario:

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

2. Libraries:

os、datetime、threading

(1) Use the threading.Timer() timer to implement timing tasks

Timer method illustrate
Timer(interval, function, args=None, kwargs=None) create timer
cancel() cancel timer
start() Execute using threads
join(self, timeout=None) Wait for thread execution to end

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

3. Run the script

jmeter execute command line

jmeter -n -t 脚本名称.jmx -l 脚本报告名称.jtl

Parameter Description:

n Non-GUI mode, command line mode (indicates running in non-GUI mode)
-t test file, the jmeter test script file to be run (generally use an absolute path)
-l result file, the file that records the result
-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 the agent in non-gui mode)
- e Set the test report to be generated after the test is completed
-o Specify the folder to generate the test report, the folder must be empty/non-existent
-H proxy host (set the proxy host used by jmeter)
-P proxy port (set the proxy port used by jmeter)
-X exit (exit at the end of the test in non-gui mode)

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()

Results of the: 

 

5.Python commonly used scheduled tasks:

while True: +sleep()
threading.Timer timer
Timeloop library executes timing tasks.
Scheduling module sched
scheduling module schedule
task framework APScheduler
distributed message system celery executes timing tasks
Use the timing tasks that come with windows

6. Four ways to call the command line with python

(1)os.system

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

Running the program will display the output, 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, and
file.readlines() is the return value of the command

(3)subprocess

You can create a subprocess in a python program,
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. Return the program exit code, similar to os.system

subprocess.check_output('ls')

Return standard output, similar to os.popen.

You can also call the Popen object to operate. subprocess

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

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

(4)commands

import commands
 commands.getoutput('ls')
  • return program output

Guess you like

Origin blog.csdn.net/u013302168/article/details/130217503