1. 程式人生 > >python 建立多個執行緒並啟動

python 建立多個執行緒並啟動


建立多個執行緒並啟動這些執行緒          th_num : 15

th_num = conf["crawl_threads"]
self._thread_list = []

self._thread_states = [None,] * th_num
for tid in range(th_num):
    th = threading.Thread(target=self.crawl_process, args=(tid,) )
    self._thread_list.append(th)
    th.start()


判斷此執行緒是否存在,若不存在 則新建並啟動執行緒

if self._thread_handle != None:
    raise ValueError, "Loader has already started"
self._thread_handle = threading.Thread(target=self._loop_scan, args=())

self._scan_flag = True
self._thread_handle.start()