refer
- daemon thread 和守護進程沒什麼關係
- key feature
- background task --> Once join() is used , whether deamon attribute is True is not important
- only useful when the main program is running
- ok to kill
- daemon process
multi thread
- GIL 鎖住的是解釋器
- IO密集型
multi core
- 計算密集型
comparasion( same & not same )
same
.start()
.run()
.join()
- 不加join都默認殺不掉,而且主進程很大可能會亂序執行(跟子進程/線程混在一起)
not same
- porcess
pool= mp.Pool( POOLSIZE:int=0 ) # 0 means use all cpu
results =[]
# method 1
results = pool.map( target , args:iterable of tuples )
for res in results :
print(res)
# method 2
for i in range( x ) :
results .append(pool.apply_async(target , args)) # 非阻塞
results .append(pool.apply(target , args:tuple)) # 阻塞
pool.close() # 關閉進程池,表示不能在往進程池中添加進程
pool.join() # 等待進程池中的所有進程執行完畢,必須在close()/terminate()之後調用
for res in results :
print(res.get()) # 注意這裏的get哦,因爲這裏返回的是一個結果對象
- apply_async和apply的區別,懶人可以直接看人家的實驗結果
- 也有第三種方法就是pool.map(func, iterable[, chunksize]),它會使進程阻塞與此直到結果返回
map_async是非阻塞的 - process類的構造函數
init(self, group=None, target=None, name=None, args=(), kwargs={})
tips
thread-safe data structure
- Queue , Value ,Array