python多進程使用函數(shù)封裝實例
我就廢話不多說了,直接看代碼吧!
import multiprocessing as mp from multiprocessing import Process class MyProcess(Process): """ 自定義多進程,繼承自原生Process,目的是獲取多進程結(jié)果到queue """ def __init__(self, func, args, q): super(MyProcess, self).__init__() self.func = func self.args = args self.res = '' self.q = q #self._daemonic = True #self._daemonic = True def run(self): self.res = self.func(*self.args) self.q.put((self.func.__name__, self.res)) def use_multiprocessing(func_list): #os.system('export PYTHONOPTIMIZE=1') # 解決 daemonic processes are not allowed to have children 問題 q = mp.Queue() # 隊列,將多進程結(jié)果存入這里,進程間共享, 多進程必須使用 multiprocessing 的queue proc_list = [] res = [] for func in func_list: proc = MyProcess(func['func'], args=func['args'], q=q) proc.start() proc_list.append(proc) for p in proc_list: p.join() while not q.empty(): r = q.get() res.append(r) return res
使用時候,將需要多進程執(zhí)行的函數(shù)和函數(shù)的參數(shù)當(dāng)作字段,組成個list 傳給use_multiprocessing 方法即可
補充知識:python一個文件里面多個函數(shù)同時執(zhí)行(多進程的方法,并發(fā))
看代碼吧!
#coding=utf-8 import time from selenium import webdriver import threading def fun1(a): print a def fun2(): print 222 threads = [] threads.append(threading.Thread(target=fun1,args=(u'愛情買賣',))) threads.append(threading.Thread(target=fun2)) print(threads) if __name__ == '__main__': for t in threads: t.setDaemon(True) #我拿來做selenium自動化模擬多個用戶使用瀏覽器的時候,加了這個就啟動不了,要去掉 t.start() import threading
首先導(dǎo)入threading 模塊,這是使用多線程的前提。
threads = [] t1 = threading.Thread(target=fun1,args=(u'愛情買賣',)) threads.append(t1)
創(chuàng)建了threads數(shù)組,創(chuàng)建線程t1,使用threading.Thread()方法,在這個方法中調(diào)用music方法target=music,args方法對music進行傳參。 把創(chuàng)建好的線程t1裝到threads數(shù)組中。
接著以同樣的方式創(chuàng)建線程t2,并把t2也裝到threads數(shù)組。
for t in threads: t.setDaemon(True) t.start()
最后通過for循環(huán)遍歷數(shù)組。(數(shù)組被裝載了t1和t2兩個線程)
setDaemon()
setDaemon(True)將線程聲明為守護線程,必須在start() 方法調(diào)用之前設(shè)置,如果不設(shè)置為守護線程程序會被無限掛起。子線程啟動后,父線程也繼續(xù)執(zhí)行下去,當(dāng)父線程執(zhí)行完最后一條語句print "all over %s" %ctime()后,沒有等待子線程,直接就退出了,同時子線程也一同結(jié)束。
start()
開始線程活動。
后記:
搞了個并發(fā)瀏覽器操作,
如果要做參數(shù)化,用ddt會導(dǎo)致所有行為都在一個瀏覽器操作,去掉ddt框架后,并發(fā)正常
以上這篇python多進程使用函數(shù)封裝實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實現(xiàn)將Word文檔中的文字轉(zhuǎn)換成語音的操作步驟
在Python中實現(xiàn)文字轉(zhuǎn)語音(Text-to-Speech, TTS)功能,能夠廣泛應(yīng)用于多種場景,如語音助手、有聲讀物、無障礙閱讀等,本文將結(jié)合具體案例,詳細介紹如何在Python中實現(xiàn)文字轉(zhuǎn)語音功能,需要的朋友可以參考下2024-08-08詳解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
這篇文章主要介紹了詳解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04解決python3 json數(shù)據(jù)包含中文的讀寫問題
這篇文章主要介紹了解決python3 json數(shù)據(jù)包含中文的讀寫問題,需要的朋友可以參考下2021-05-05在Python的Tornado框架中實現(xiàn)簡單的在線代理的教程
這篇文章主要介紹了在Python的Tornado框架中實現(xiàn)簡單的在線代理的教程,代理功能是一個常見的網(wǎng)絡(luò)編程實現(xiàn),需要的朋友可以參考下2015-05-05Python?sklearn預(yù)測評估指標(biāo)混淆矩陣計算示例詳解
這篇文章主要為大家介紹了Python?sklearn預(yù)測評估指標(biāo)混淆矩陣計算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02打包Python代碼的常用方法實現(xiàn)程序exe應(yīng)用
Python是一門強大的編程語言,但在將Python代碼分享給其他人時,讓他們安裝Python解釋器并運行腳本可能有點繁瑣,這時,將Python代碼打包成可執(zhí)行的應(yīng)用程序(.exe)可以大大簡化這個過程,本文將介紹幾種常用的方法,輕松地將Python代碼變成獨立的可執(zhí)行文件2023-12-12