PyQt5线程队列-----ThreadLoop

import sys
import os

from PyQt5.QtCore import QObject, QDateTime
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QGuiApplication

"""模块路径加到当前模块扫描的路径里"""
sys.path.append(os.path.dirname(__file__))
# import numpy as np
# import cv2
"""
@项目 :ThreadLoop
@文件 :LinkList.py
@作者 :xsh
@时间 :2021/11/21 15:47
@功能 :
@说明 :

"""
from LinkNode import myThread, LinkNode


class LinkList(QObject):
MaxLen: int # 链表最大长度
Size: int # 当前长度
Head: LinkNode # 头节点
Tail: LinkNode # 尾节点
Current: LinkNode # 当前节点
Info: str # 节点信息

def __init__(self, info='', maxLen=int(50), parent=None):
super(LinkList, self).__init__(parent)
self.Info = info
self.MaxLen = maxLen
self.Size = 0
self.Head = None # 头节点
self.Tail = None # 尾节点
self.Current = None # 当前节点
self.setMaxLen(maxLen)


def __initAllValue(self):
self.Size = 0
self.Head = LinkNode(self.Size - 1)
self.Tail = LinkNode(self.Size - 2)
self.Current = LinkNode(self.Size)

self.Head.LastNode = self.Tail
self.Head.NextNode = self.Current

self.Current.LastNode = self.Head
self.Current.NextNode = self.Tail

self.Tail.LastNode = self.Current
self.Tail.NextNode = self.Head

"""删除所有节红框"""

def __delAllNode(self):
if self.Head is None:
return
Head = self.Head

while Head.NextNode is not None:
oldNode = Head
Head = Head.NextNode
self.__deleteNode(oldNode)
self.Size -= 1
self.__deleteNode(Head)
self.Size = 0

"""删除指定的节点"""

def __deleteNode(self, Node: LinkNode):
Node.Stop()
while Node.isRunning():
pass
del Node
Node = None

"""追加一个节点"""

def appendNode(self):
if self.Current == self.Head:
self.Current = self.Head.NextNode
if self.Current == self.Tail:
self.Current = self.Head.NextNode
CurrentIndex = self.Current.CurrentThreadNumber + 1
NewNode = LinkNode(CurrentIndex)
NewNode.NextNode = self.Current.NextNode
NewNode.LastNode = self.Current
self.Current.NextNode = NewNode
ChangeNode = NewNode.NextNode
while ChangeNode != self.Tail:
CurrentIndex += 1
ChangeNode.CurrentThreadNumber = CurrentIndex
ChangeNode = ChangeNode.NextNode
self.Current = NewNode
self.Size += 1

"""播入一个节点"""

def insertNode(self):
if self.Current == self.Head:
self.Current = self.Head.NextNode
if self.Current == self.Tail:
self.Current = self.Head.NextNode
CurrentIndex = self.Current.CurrentThreadNumber
NewNode = LinkNode(CurrentIndex)
NewNode.LastNode = self.Current.LastNode
NewNode.NextNode = self.Current

self.Current.LastNode = NewNode
ChangeNode = self.Current
while ChangeNode != self.Tail:
CurrentIndex += 1
ChangeNode.CurrentThreadNumber = CurrentIndex
ChangeNode = ChangeNode.NextNode

self.Current = NewNode
self.Size += 1

"""暂停所有节点"""

def PauseAllNode(self):
Head = self.Head
while Head != self.Tail:
Head.Pause()
Head = Head.NextNode

"""停止所有节点线程"""

def StopAllNode(self):
Head = self.Head
while Head != self.Tail:
Head.Stop()
Head = Head.NextNode

"""根据最大线程长度,初始化链表"""

def InitNode(self):
if self.MaxLen < 1:
return
while self.Size < self.MaxLen:
self.appendNode()
self.Current = self.Head.NextNode
print("当前时间:{0} 创建{1}成功, 长度为:{2}".format(
QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"),
self.Info,
self.MaxLen))
def setMaxLen(self, maxLen=50):
if maxLen < 1:
return
self.MaxLen = maxLen
self.__delAllNode()
self.__initAllValue()
self.InitNode()

def Start(self, *args):
Current = self.Current
if Current.isRunning():
if not Current.isFree():
while not Current.isFree():
if Current == self.Tail:
Current = self.Head.NextNode
if Current == self.Head:
Current = self.Head.NextNode
Current = Current.NextNode
Current.Start(*args)
self.Current = Current
def DrawNode(self):
Head = self.Head
while Head != self.Tail:
print("当前线程ID:{0}-{1}".format(Head.CurrentThreadNumber, Head.CurrentThread.currentThreadId()))
Head = Head.NextNode
print("当前线程ID:{0}-{1}".format(Head.CurrentThreadNumber, Head.CurrentThread.currentThreadId()))
上一篇:链表与数组


下一篇:初学者学习链表应该看的第一篇文章(c语言)