常规的多线程代码:
import threading
runWebVedio(i):
...
fun_timer(x):
...
th1 = threading.Thread(target= runWebVedio, args=(1,), name="thread1")
th2 = threading.Thread(target= fun_timer, args=(x,), name="thread2")
th1.start()
th2.start()
但是几个线程没有先后执行顺序。
上一篇文章提到多线程按顺序执行,
python多线程按顺序执行_xue_csdn的博客-CSDN博客3种不同的队列类q=Queue(maxsize):创建一个FIFO(first-in first-out,先进先出)队列。maxsize是队列中金额以放入的项的最大数量。如果省略maxsize参数或将它置为0,队列大小将无穷大。q=LifoQueue(maxsize):创建一个LIFO(last-in first-out,后进先出)队列(栈)。q=PriorityQueue(maxsize):创建一个优先级队列,其中项按照优先级从低到高依次排好。使用这种队列时,项应该是(priority,https://blog.csdn.net/xue_csdn/article/details/121283312如果第一个线程的优先级比较高,但是执行周期比第二个线程的周期长(比如第一个线程每5秒钟执行一次,第二个每1秒执行一次),那么第二个线程必须等前一个线程执行完之后,才会按照顺序执行,这样会导致第二个线程一直在等待,执行不了。
所以,按照线程的执行周期,在每个线程中添加定时任务,每隔一定时间重复执行某个操作。
import threading
def fun_timer():
print('Hello Timer!')
global timer
timer = threading.Timer(5, fun_timer)
timer.start()
timer = threading.Timer(1, fun_timer)
timer.start()
上述程序运行一秒后执行fun_timer()函数,此后每隔五秒执行一次。