threading模块
.thread(target = 目标函数 , args = (参数1,参数2,.....))
threading.current_thread().name 获取当前线程的名称
.start()启动线程
.join()要求主线程等待
.name 线程名称
import time
import threading
def worker(n):
print('{}函数执行开始于:{}'.format(threading.current_thread().name ,time.ctime()))
time.sleep(n)
print('{}函数执行结束于:{}'.format(threading.current_thread().name ,time.ctime()))
def main():
print('【主函数执行开始于:{}】'.format(time.ctime()))
threads=[] #放到数组里的目的是想让线程一起启动
t1=threading.Thread(target=worker,args=(4,))
threads.append(t1)
t2=threading.Thread(target=worker,args=(2,))
threads.append(t2)
for t in threads:
t.start()
for t in threads:
t.join()# 让线程等待
print('【主函数执行结束于:{}】'.format(time.ctime()))
if __name__=='__main__':
main()
"""
【主函数执行开始于:Sat Feb 16 07:44:29 2019】
Thread-1函数执行开始于:Sat Feb 16 07:44:29 2019
Thread-2函数执行开始于:Sat Feb 16 07:44:29 2019
Thread-2函数执行结束于:Sat Feb 16 07:44:31 2019
Thread-1函数执行结束于:Sat Feb 16 07:44:33 2019
【主函数执行结束于:Sat Feb 16 07:44:33 2019】
"""
使用threading.Thread 派生类实现多线程
import time
import threading
"""threading.Therad 派生类实现多线程
"""
def worker(n):
print('{}函数执行开始于:{}'.format(threading.current_thread().name ,time.ctime()))
time.sleep(n)
print('{}函数执行结束于:{}'.format(threading.current_thread().name ,time.ctime()))
class MyThread(threading.Thread):
def __init__(self,func,args):
threading.Thread.__init__(self)
self.func=func
self.args=args
def run(self):
self.func(*self.args) # *是解包
def main():
print('【主函数执行开始于:{}】'.format(time.ctime()))
threads=[] #放到数组里的目的是想让线程一起启动
t1=MyThread(worker,(4,))
# t1=threading.Thread(target=worker,args=(4,))
threads.append(t1)
t2=MyThread(worker,(2,))
#t2=threading.Thread(target=worker,args=(2,))
threads.append(t2)
for t in threads:
t.start()
for t in threads:
t.join()# 让线程等待
print('【主函数执行结束于:{}】'.format(time.ctime()))
if __name__=='__main__':
main()
"""
【主函数执行开始于:Sat Feb 16 07:45:33 2019】
Thread-1函数执行开始于:Sat Feb 16 07:45:33 2019
Thread-2函数执行开始于:Sat Feb 16 07:45:33 2019
Thread-2函数执行结束于:Sat Feb 16 07:45:35 2019
Thread-1函数执行结束于:Sat Feb 16 07:45:37 2019
【主函数执行结束于:Sat Feb 16 07:45:37 2019】
"""
同步原语(锁)
.acquire()获得
.release()释放
with lock: 支持上下文操作
import threading
import time
import random
"""同步原语之:锁
"""
eggs=[]
lock=threading.Lock()
def put_egg(n,lst):
# lock.acquire() 获得
with lock:
for i in range(1,n+1):
time.sleep(random.randint(0,2))
lst.append(i)
#lock.release() 释放
def main():
threads=[]
for i in range(3): #3个人放鸡蛋
t=threading.Thread(target=put_egg,args=(5,eggs)) #将5个鸡蛋依次放入
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(eggs)
if __name__ == '__main__':
main()
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
队列
queue 模块
- 先进先出队列 FIFO
- 后进先出的队列 LIFO , 其他语言的栈
- PriorityQueue 优先队列
Queue FIFO
#coding=gbk
import threading
import queue
import time
import random
""" FIFO 队列示例
"""
def producer(data_queue):
for i in range(5):
time.sleep(0.5)
item=random.randint(1,100)
data_queue.put(item)
print('{}在队列中放入数据项:{}'.format(threading.current_thread().name ,item))
def consumer(data_queue):
while True:
try:
item=data_queue.get(timeout=3)
print('{}从队列中移除了{}'.format(threading.current_thread().name ,item))
except queue.Empty:
break
else:
data_queue.task_done()
def main():
q=queue.Queue()
threads=[]
p=threading.Thread(target=producer,args=(q,))
p.start()
for i in range(2):
c=threading.Thread(target=consumer,args=(q,))
threads.append(c)
for t in threads:
t.start()
for t in threads:
t.join()
q.join()
if __name__ == '__main__':
main()
multiprocessing多进程模块:
充分运用多核、多cpu的计算能力,适用于计算密集型任务
concurrent.futures 模块
#coding=gbk
import time
import concurrent.futures
""" concurrent.futures模块的学习。
"""
number = list(range(1,11))
def count(n):
for i in range(1000000):
i += i
return i * n
def worker(x):
result = count(x)
print("数字:{}的计算结果是:{}".format(x,result))
#顺序执行
def sequential_execution():
start_time = time.clock()
for i in number:
worker(i)
print('顺序执行花费时间:{}秒'.format(time.clock() - start_time))
#线程池执行
def threading_execution():
start_time = time.clock()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for i in number:
executor.submit(worker,i)
print('线程池的花费时间:{}秒'.format(time.clock() - start_time))
#多进程去执行
def process_excution():
start_time = time.clock()
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executer:
for i in number:
executer.submit(worker,i)
print('进程池执行花费的时间为:{}秒'.format(time.clock() - start_time))
if __name__ == '__main__':
#sequential_execution() #顺序执行花费时间:0.7226814389999999秒
#threading_execution() #线程池的花费时间:0.815289691
process_excution() #进程池执行花费的时间为:0.899161747秒
- ThreadPoolExecutor线程池的模块
- ProcessPoolExecutor多进行的模块