运行模块时会有一些条件
有一个专门存储运行任务的列表
当需要运行3次任务时,可以使用次数条件
当需要满足某个任务运行后,再运行任务,可以使用任务条件
设置运行次数,任务运行设置的次数
def count_task(self, condition, task):
self.tasks.append(task.get_name)
for i in range(condition):
task.start()
self.tasks.remove(task.get_name)
当某个任务在运行中时,这个任务开始运行
def work_task(self, condition, task):
while True:
if condition in self.tasks:
self.tasks.append(task.get_name)
task.start()
self.tasks.remove(task.get_name)
return 1
else:
break
两种条件运行,使用线程启动
def count_run(self, condition, task):
t = threading.Thread(target=self.count_task, args=(condition,task,))
t.start()
def work_run(self, condition, task):
t = threading.Thread(target=self.work_task, args=(condition,task,))
t.start()
import threading
class Runner:
def __init__(self):
self.tasks = []
def count_task(self, condition, task):
self.tasks.append(task.get_name())
for i in range(condition):
task.start()
self.tasks.remove(task.get_name())
def work_task(self, condition, task):
while True:
if condition in self.tasks:
self.tasks.append(task.get_name())
task.start()
self.tasks.remove(task.get_name())
return 1
else:
break
def count_run(self, condition, task):
t = threading.Thread(target=self.count_task, args=(condition,task,))
t.start()
def work_run(self, condition, task):
t = threading.Thread(target=self.work_task, args=(condition,task,))
t.start()
写一个任务类
class Task:
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
def start(self):
print(self.name,"运行了")
运行
task1 = Task("a")
task2 = Task("b")
task3 = Task("c")
runner = Runner()
runner.work_run("c", task1)
runner.count_run(109, task2)
runner.count_run(100, task3)