工作队列(又称:任务队列——Task Queues)是为了避免等待一些占用大量资源、时间的操作。当我们把任务(Task)当作消息发送到队列中,一个运行在后台的工作者(worker)进程就会取出任务然后处理。当你运行多个工作者(workers),任务就会在它们之间共享。这个概念在网络应用中是非常有用的,它可以在短暂的HTTP请求中处理一些复杂的任务,我么可以将耗时的请求放在任务队列,然后立马返回响应,接下来由多个worker去处理复杂的业务操作。(这种架构叫做"分布式异步队列",有时候用来方式D-DOS攻击,12306网站就是采用这种模式)
一、简单创建线程及@用类起多线程
# 线程实例--用类来起多线程
class myThread(threading.Thread):
def __init__(self, threadID, name, delay):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.delay = delay
def run(self):
print('开启线程' + self.name)
threadLock.acquire()
parint_time(self.name, self.delay, 30)
threadLock.release()
class myAThread(threading.Thread):
def __init__(self, threadID, name, delay):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.delay = delay
def run(self):
print('开启线程' + self.name)
threadLock.acquire()
ppint_time(self.name, self.delay, 30)
threadLock.release()
if __name__ == '__main__':
# 线程同步 执行run方法---用类来起多线程
thread1 = myThread(1, "Thread-1", 0) # 创建新线程
thread2 = myAThread(2, "Thread-2", 0)
thread3 = myAThread(3, "Thread-3", 0)
thread1.start() # 开启新线程
thread2.start()
thread3.start()
threads.append(thread3) # 添加线程到线程列表
threads.append(thread1) # 添加线程到线程列表
threads.append(thread2)
for tt in threads:
tt.join() # 等待所有线程完成
二、简单创建线程及@用函数起多线程
def parint_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s:%s" % (threadName, time.ctime(time.time())))
counter -= 1
def ppint_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s:%s" % (threadName, time.ctime(time.time())))
counter -= 1
# 线程队列
if __name__ == '__main__':
# # 线程同步 执行指定方法---用函数来起多线程
thread4 = threading.Thread(target=parint_time("Thread-4", 0, 2), args=())
thread4.start()
thread5 = threading.Thread(target=ppint_time("Thread-5", 0, 2), args=())
thread5.start()
threads.append(thread4)
threads.append(thread5)
for tt in threads:
tt.join() # 等待所有线程完成
print('END')
三、队列
跟多线程没直接关系,queue队列支持先进先出(默认)、也支持先进后出等;
exitFlag = 0
threadLock = threading.Lock()
class myThreadS(threading.Thread):
# 继承父类
def __init__(self, threadID, name, q):
# threadID:线程ID
# name:线程名称
# q:队列
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print("Starting " + self.name)
process_data(self.name, self.q)
print("Exiting " + self.name)
def process_data(threadName, q):
# threadName:线程名称
# q:队列
while not exitFlag:
threadLock.acquire()
print(workQueue.empty())
if not workQueue.empty():
data = q.get()
threadLock.release()
print("%s processing %s" % (threadName, data))
else:
threadLock.release()
time.sleep(1)
# 线程队列
if __name__ == '__main__':
# 创建
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
# 定义线程ID
threadID = 1
threads = []
# 定义队列,长度为2
workQueue = queue.Queue(2)
# 判断是否满队列
if workQueue.full():
print(workQueue.full())
for tName in threadList:
# 重写方法,创建线程
thread = myThreadS(threadID, tName, workQueue)
# 执行方法
thread.start()
# 添加到线程池
threads.append(thread)
threadID += 1
print(tName)
# 获得锁
threadLock.acquire()
# 填充队列
for word in nameList:
print(word)
workQueue.put(word)
# 释放锁
threadLock.release()
# 等待队列清空
while not workQueue.empty():
pass
exitFlag = 1
for t in threads:
t.join()
print('*****************End********************')