Python實(shí)現(xiàn)的多進(jìn)程和多線程功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)的多進(jìn)程和多線程功能。分享給大家供大家參考,具體如下:
聽了朋友說起,他們目前開發(fā)的測試框架,用python實(shí)現(xiàn)的分布式系統(tǒng)。雖然python的執(zhí)行效率沒有c和c++那么高,但是依靠集群的力量,產(chǎn)生的壓力很是牛逼啊。
了解了下大概的方式就是
1、有臺主控機(jī),負(fù)責(zé)調(diào)度,比如執(zhí)行的參數(shù)等
2、有n多臺執(zhí)行機(jī),每個執(zhí)行機(jī)上部署一個python的xmlRPC server,主控機(jī)調(diào)用rpccall,然后執(zhí)行機(jī)執(zhí)行。rpccall里面會fork一些進(jìn)程,每個進(jìn)程再創(chuàng)建一些線程,去調(diào)用測試方法。這樣,擴(kuò)展性就很好了。
對于python的rpc call,之前也沒有接觸過,不是很了解,google了一下,發(fā)現(xiàn)很簡單,拿了個網(wǎng)上的例子,如下,先部署一個rpcServer
from SimpleXMLRPCServer import SimpleXMLRPCServer
def add(a , b):
return a+bserver = SimpleXMLRPCServer(("10.249.192.38", 8000))#這里不要用localhost,否則只有本機(jī)才能訪問
server.register_function(add)
server.serve_forever()
客戶端:
from xmlrpclib import Server
Proxyserver = ServerProxy("http://localhost:8000")
try: ret = server.add(30,90) print 'result:', ret print 'result type:', type(ret)
except
Exception, e: print "exception",e
其實(shí)還是很簡單的。
然后再看了下python的多進(jìn)程和多線程的方法,寫了個例子,如下:
#encoding=utf-8
import sys
import os
import time
import pdb
import httplib
import thread
import threading
constant_p = 0 #創(chuàng)建全局變量,進(jìn)程數(shù)
constant_s = 0 #創(chuàng)建全局變量,線程數(shù)
mutex_g = threading.RLock() #創(chuàng)建全局鎖
def run(count):#該函數(shù)創(chuàng)建3個線程,同時調(diào)用3個不同的函數(shù)
a = 1
b = 0
thread.start_new_thread(test0,(a,b))#這里傳入的參數(shù)需要以元組的形式,跟void指針其實(shí)也差不多
thread.start_new_thread(test1,(a,b))
thread.start_new_thread(test2,(a,b))
def test0(a,b):
global mutex_g
global constant_s
threadid = thread.get_ident()
mutex_g.acquire()#這里需要把線程數(shù)說鎖起來,否則結(jié)果會被修改
constant_s = constant_s+1
mutex_g.release()
print "thread 0 called,and the threadid is:%d"%(threadid)
sys.exit(0)
def test1(a,b):
global mutex_g
global constant_s
threadid = thread.get_ident()
mutex_g.acquire()
constant_s = constant_s+1
mutex_g.release()
print "thread 1 called,and the threadid is:%d"%(threadid)
sys.exit(0)
def test2(a,b):
global mutex_g
global constant_s
threadid = thread.get_ident()
mutex_g.acquire()
constant_s = constant_s+1
mutex_g.release()
print "thread 2 called,and the threadid is:%d"%(threadid)
sys.exit(0)
def my_fork():
global constant_p
pid = os.fork()#fork一個子進(jìn)程,子進(jìn)程的pid=0同時2個進(jìn)程會執(zhí)行my_fork()函數(shù)
if (pid == 0):#子進(jìn)程執(zhí)行到這個if里面
constant_p = constant_s + 1
run(3)
time.sleep(5)
print "total thread is %d"%constant_s#這個結(jié)果是3,因?yàn)樽舆M(jìn)程創(chuàng)建按了3個線程
elif (pid >0):#父進(jìn)程執(zhí)行到這個if里面
constant_p = constant_s + 1 run(4)
time.sleep(5)
print "total thread is %d"%constant_s#這個結(jié)果也是3,因?yàn)樵撨M(jìn)程也創(chuàng)建了3個線程
else:
print "fork error"
sys.exit(0)
print "total process is %d"%constant_p#該結(jié)果是1,因?yàn)?個進(jìn)程只執(zhí)行了一次
constant_p = constant_s + 1
sys.exit(0)
if __name__ == "__main__":
my_fork()
#my_fork()
#my_fork()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python 多進(jìn)程和多線程使用詳解
- Python全局鎖中如何合理運(yùn)用多線程(多進(jìn)程)
- python線程安全及多進(jìn)程多線程實(shí)現(xiàn)方法詳解
- Python實(shí)現(xiàn)多線程/多進(jìn)程的TCP服務(wù)器
- python多線程與多進(jìn)程及其區(qū)別詳解
- Python實(shí)現(xiàn)的服務(wù)器示例小結(jié)【單進(jìn)程、多進(jìn)程、多線程、非阻塞式】
- Python中單線程、多線程和多進(jìn)程的效率對比實(shí)驗(yàn)實(shí)例
- Python多線程處理實(shí)例詳解【單進(jìn)程/多進(jìn)程】
- Python并發(fā):多線程與多進(jìn)程的詳解
- Python多線程與多進(jìn)程相關(guān)知識總結(jié)
相關(guān)文章
Cpython編譯后再使用Pyinstaller打包的詳細(xì)教程
pyinstaller是一個第三方庫,它能夠在Windows、Linux、 Mac OS X 等操作系統(tǒng)下將 Python 源文件打包,通過對源文件打包,這篇文章主要介紹了Cpython編譯后再使用Pyinstaller打包的詳細(xì)教程,需要的朋友可以參考下2023-11-11
利用python繪制數(shù)據(jù)曲線圖的實(shí)現(xiàn)
這篇文章主要介紹了利用python繪制數(shù)據(jù)曲線圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python基于numpy靈活定義神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的方法
這篇文章主要介紹了Python基于numpy靈活定義神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的方法,結(jié)合實(shí)例形式分析了神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的原理及Python具體實(shí)現(xiàn)方法,涉及Python使用numpy擴(kuò)展進(jìn)行數(shù)學(xué)運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
tensorboard 可以顯示graph,卻不能顯示scalar的解決方式
今天小編就為大家分享一篇tensorboard 可以顯示graph,卻不能顯示scalar的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評估
這篇文章主要介紹了Python+OpenCV使用攝像頭追蹤人臉面部血液變化實(shí)現(xiàn)脈搏評估,本文通過一段代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Python虛擬機(jī)之super超級魔法的使用和工作原理詳解
在本篇文章中,我們將深入探討Python中的super類的使用和內(nèi)部工作原理,super類作為Python虛擬機(jī)中強(qiáng)大的功能之一,super 可以說是 Python 對象系統(tǒng)基石,他可以幫助我們更靈活地使用繼承和方法調(diào)用,需要的朋友可以參考下2023-10-10

