python如何開啟多線程
python開啟多線程
為了加快程序運(yùn)行速度,對(duì)相同功能的一些執(zhí)行語句,python可以通過 ThreadPool 做到
重要的函數(shù)為
pool = ThreadPool(processes=3) pool.apply_async(func, args=(**krags)) pool.close() pool.join()
from multiprocessing.pool import ThreadPool def parallel(self, cls, driven_data_key=None): ? ? if not self.FINAL_TEMPLATE: ? ? ? ? self.get_final_templates() ? ? # 開啟線程池里線程的數(shù)量 ? ? pool = ThreadPool(processes=len(self.FINAL_TEMPLATE)) ? ? # 當(dāng)前的for循環(huán)實(shí)則并行執(zhí)行 ? ? for top_template in self.FINAL_TEMPLATE: ? ? ?? ?# 第一個(gè)參數(shù)為想要并行執(zhí)行的函數(shù),第二個(gè)參數(shù)為要執(zhí)行的函數(shù)所需要的參數(shù) ? ? ? ? pool.apply_async(self.filter_case_online, args=(cls, top_template)) ? ? pool.close() ? ? pool.join()
python開啟多線程/停止多線程
import ctypes import inspect import threading import time def main(a): while True: print(a) class myThread(threading.Thread): # 繼承父類threading.Thread def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): # 把要執(zhí)行的代碼寫到run函數(shù)里面 線程在創(chuàng)建后會(huì)直接運(yùn)行run函數(shù) main(self.name) def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # """if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect""" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit) if __name__ == '__main__': nameList = [1, 2, 3, 4, 5, 6] threadList = [] for name in nameList: threadList.append(myThread(str(name))) # 開啟線程 for thread in threadList: thread.start() # 停止線程 time.sleep(1) for thread in threadList: stop_thread(thread)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python常用時(shí)間操作總結(jié)【取得當(dāng)前時(shí)間、時(shí)間函數(shù)、應(yīng)用等】
這篇文章主要介紹了Python常用時(shí)間操作,包括取得當(dāng)前時(shí)間、時(shí)間函數(shù)、應(yīng)用等概念與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05Python查找數(shù)組中數(shù)值和下標(biāo)相等的元素示例【二分查找】
這篇文章主要介紹了Python查找數(shù)組中數(shù)值和下標(biāo)相等的元素,結(jié)合實(shí)例形式分析了Python基于二分查找針對(duì)數(shù)組查找相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02PyTorch深度學(xué)習(xí)LSTM從input輸入到Linear輸出
這篇文章主要為大家介紹了PyTorch深度學(xué)習(xí)LSTM從input輸入到Linear輸出深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05pandas factorize實(shí)現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征
今天小編就為大家分享一篇pandas factorize實(shí)現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python實(shí)現(xiàn)將PDF文件拆分任意頁數(shù)
PyMuPDF,簡(jiǎn)稱fitz,是一個(gè)輕量級(jí)的Python庫,它簡(jiǎn)化和封裝了PyMuPDF的功能,使得在Python中處理PDF文件更加簡(jiǎn)單,下面我們來看看如何使用他將PDF拆分任意頁數(shù)2025-02-02Python利用redis-py實(shí)現(xiàn)集合與有序集合的常用指令操作
這篇文章我們將來學(xué)習(xí)?redis-py?這個(gè)模塊針對(duì)?“集合”?與?"有序集合"的一些常用指令操作,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-09-09關(guān)于matplotlib及相關(guān)cmap參數(shù)的取值方式
這篇文章主要介紹了關(guān)于matplotlib及相關(guān)cmap參數(shù)的取值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Python如何爬取51cto數(shù)據(jù)并存入MySQL
這篇文章主要介紹了Python如何爬取51cto數(shù)據(jù)并存入MySQL,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08