python多线程和多进程
GIL全局锁,全局解释器锁
I/O密集型应用和CPU密集型应用
import threading
import time
class LoopThread(threading.Thread):
n = 0
def run(self):
while self.n < 5:
print(self.n)
now_thread = threading.current_thread()
print(now_thread.name)
time.sleep(1)
self.n += 1
if __name__ == '__main__':
now_thread = threading.current_thread()
print(now_thread.name)
t = LoopThread(name='loop_thead_loop')
t.start()
t.join()同步与互斥:锁机制
Lock
RLock
Lock和RLock的区别: Rlock可以重复锁
锁定的语法:
with lock:
def change_it(n):
""" 改变我的余额 """
global balance
# 方式一,使用with
with your_lock:
balance = balance + n
time.sleep(2)
balance = balance - n
time.sleep(1)
print('-N---> {0}; balance: {1}'.format(n, balance))
# 方式二
# try:
# print('start lock')
# # 添加锁
# your_lock.acquire()
# print('locked one ')
# # 资源已经被锁住了,不能重复锁定, 产生死锁
# your_lock.acquire()
# print('locked two')
# balance = balance + n
# time.sleep(2)
# balance = balance - n
# time.sleep(1)
# print('-N---> {0}; balance: {1}'.format(n, balance))
# finally:
# # 释放掉锁
# your_lock.release()
# your_lock.release()CONDITION
多线程调度和优化
import time
import threading
from concurrent.futures import ThreadPoolExecutor
from multiprocessing.pool import ThreadPool, Pool
def run(n):
"""
线程要做的事情
:param n:
:return:
"""
time.sleep(2)
print(threading.current_thread().name,n)
def main():
"""使用传统的方法来做任务"""
t1 = time.time()
for n in range(100):
run(n)
print(time.time() - t1)
def main_use_thread():
"""资源有限,使用线程优化任务"""
ls = []
t1 = time.time()
for count in range(10):
for i in range(10):
t = threading.Thread(target = run,args=(i,))
ls.append(t)
t.start()
for l in ls:
l.join()
print(time.time()-t1)
def main_use_pool():
"""使用线程池优化"""
t1 = time.time()
n_list = range(100)
pool = Pool(10)
pool.map(run,n_list)
pool.close()
pool.join()
print(time.time() -t1)
def main_use_executor():
t1 = time.time()
n_list = range(100)
with ThreadPoolExecutor(max_workers=10) as executor:
executor.map(run,n_list)
print(time.time()-t1)
if __name__ == '__main__':
# main()
# main_use_thread()
# main_use_pool()
main_use_executor()
凡岛公司福利 294人发布