我必須合并兩個串列,每次完整的串列才能合并它們,但是我這樣做是怎么回事:
def repeated_fill_buffer(self):
"""
repeat the operation until reaching the end of file
"""
# clear buffers from last data
self.block = [[] for file in self.files]
filling_buffer_thread = threading.Thread(self.fill_buffer())
filling_buffer_thread.start()
# create inverted index thread
create_inverted_index_thread = threading.Thread(self.create_inverted_index())
create_inverted_index_thread.start()
# check if buffers are not empty to merge and start the thread
if any(self.block):
self.block = [[] for file in self.files]
filling_buffer_thread.join()
create_inverted_index_thread.join()
但是發生了什么,filling_buffer_thread只是create_inverted_index_thread呼叫了一次,并且沒有再次作業,當我除錯代碼時,我看到了
填充緩沖區執行緒已停止
我不知道我是否很好地解釋了我的問題,但我想要的是我可以多次呼叫同一個執行緒并運行它們..
uj5u.com熱心網友回復:
如果有任何 CPU Bound 的操作,那么使用執行緒是沒有用的。由于 Python GIL,它可以防止一次執行多個位元組碼指令。使用多處理模塊,因為每個行程都有自己的 GIL。
所有數字運算或任何依賴 CPU 完成的操作都是 CPU 系結的。執行緒對于 I/O 系結操作(如資料庫呼叫、網路呼叫)很有用
總結您的錯誤,your filling_buffer_thread由于create_inverted_index_thread
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517037.html
上一篇:__sync_local_test_and_set/xchg可以用來實作互斥鎖嗎?
下一篇:多執行緒會幫助快速傅立葉變換嗎?
