Python實現的多進程和多線程功能示例
本文實例講述了Python實現的多進程和多線程功能。分享給大家供大家參考,具體如下:
聽了朋友說起,他們目前開發(fā)的測試框架,用python實現的分布式系統(tǒng)。雖然python的執(zhí)行效率沒有c和c++那么高,但是依靠集群的力量,產生的壓力很是牛逼啊。
了解了下大概的方式就是
1、有臺主控機,負責調度,比如執(zhí)行的參數等
2、有n多臺執(zhí)行機,每個執(zhí)行機上部署一個python的xmlRPC server,主控機調用rpccall,然后執(zhí)行機執(zhí)行。rpccall里面會fork一些進程,每個進程再創(chuàng)建一些線程,去調用測試方法。這樣,擴展性就很好了。
對于python的rpc call,之前也沒有接觸過,不是很了解,google了一下,發(fā)現很簡單,拿了個網上的例子,如下,先部署一個rpcServer
from SimpleXMLRPCServer import SimpleXMLRPCServer
def add(a , b):
return a+bserver = SimpleXMLRPCServer(("10.249.192.38", 8000))#這里不要用localhost,否則只有本機才能訪問
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
其實還是很簡單的。
然后再看了下python的多進程和多線程的方法,寫了個例子,如下:
#encoding=utf-8
import sys
import os
import time
import pdb
import httplib
import thread
import threading
constant_p = 0 #創(chuàng)建全局變量,進程數
constant_s = 0 #創(chuàng)建全局變量,線程數
mutex_g = threading.RLock() #創(chuàng)建全局鎖
def run(count):#該函數創(chuàng)建3個線程,同時調用3個不同的函數
a = 1
b = 0
thread.start_new_thread(test0,(a,b))#這里傳入的參數需要以元組的形式,跟void指針其實也差不多
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()#這里需要把線程數說鎖起來,否則結果會被修改
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一個子進程,子進程的pid=0同時2個進程會執(zhí)行my_fork()函數
if (pid == 0):#子進程執(zhí)行到這個if里面
constant_p = constant_s + 1
run(3)
time.sleep(5)
print "total thread is %d"%constant_s#這個結果是3,因為子進程創(chuàng)建按了3個線程
elif (pid >0):#父進程執(zhí)行到這個if里面
constant_p = constant_s + 1 run(4)
time.sleep(5)
print "total thread is %d"%constant_s#這個結果也是3,因為該進程也創(chuàng)建了3個線程
else:
print "fork error"
sys.exit(0)
print "total process is %d"%constant_p#該結果是1,因為2個進程只執(zhí)行了一次
constant_p = constant_s + 1
sys.exit(0)
if __name__ == "__main__":
my_fork()
#my_fork()
#my_fork()
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Cpython編譯后再使用Pyinstaller打包的詳細教程
pyinstaller是一個第三方庫,它能夠在Windows、Linux、 Mac OS X 等操作系統(tǒng)下將 Python 源文件打包,通過對源文件打包,這篇文章主要介紹了Cpython編譯后再使用Pyinstaller打包的詳細教程,需要的朋友可以參考下2023-11-11
tensorboard 可以顯示graph,卻不能顯示scalar的解決方式
今天小編就為大家分享一篇tensorboard 可以顯示graph,卻不能顯示scalar的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
淺析Python+OpenCV使用攝像頭追蹤人臉面部血液變化實現脈搏評估
這篇文章主要介紹了Python+OpenCV使用攝像頭追蹤人臉面部血液變化實現脈搏評估,本文通過一段代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

