这里使用Python多线程的threading模块,threading模块的Thread是可被继承的类。我们可以采用继承的方法实现多线程异步执行。
import threading
import time
class TestTask(threading.Thread):
def __init__(self, value):
super().__init__()
self.value = value
def run(self):
""" 执行 """
while True:
...
if xxx:
break
time.sleep(s) # 通过While True 以及 sleep 可实现轮询功能 s-->second
使用只需要在代码中实例化TestTask对象并启动即可,如:
TestTask(value).start()