实例分析Python实现的多进程和多线程功能
- 更新时间:2020-02-22 10:27:13
- 编辑:欧欢欣
参考资料
- 《Python爬虫、数据分析与可视化:工具详解与案例实战》配书资源 配套资源 / 4.5 MB / 成立明 胡书敏 黄勇 推荐度:
- 《精通Python自动化编程》源代码 配套资源 / 4.6 MB / 黄永祥 推荐度:
- 《编程的乐趣:用Python解算法谜题》配套源码 配套资源 / 67 KB / 斯里尼·德瓦达斯 推荐度:
- Python项目开发案例集锦 PDF 电子书 / 99.1 MB / 明日科技 推荐度:
- 《Python程序设计(第3版)》教学PPT 配套资源 / 5 MB / [美] 约翰·策勒 (John Zell 推荐度:
正文内容
Python实现的多进程和多线程功能示例
本文实例讲述了Python实现的多进程和多线程功能。分享给大家供大家参考,具体如下:
听了朋友说起,他们目前开发的测试框架,用python实现的分布式系统。虽然python的执行效率没有c和c++那么高,但是依靠集群的力量,产生的压力很是牛逼啊。
了解了下大概的方式就是
1、有台主控机,负责调度,比如执行的参数等
2、有n多台执行机,每个执行机上部署一个python的xmlRPC server,主控机调用rpccall,然后执行机执行。rpccall里面会fork一些进程,每个进程再创建一些线程,去调用测试方法。这样,扩展性就很好了。
对于python的rpc call,之前也没有接触过,不是很了解,google了一下,发现很简单,拿了个网上的例子,如下,先部署一个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 #创建全局变量,进程数 constant_s = 0 #创建全局变量,线程数 mutex_g = threading.RLock() #创建全局锁 def run(count):#该函数创建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个进程会执行my_fork()函数 if (pid == 0):#子进程执行到这个if里面 constant_p = constant_s + 1 run(3) time.sleep(5) print "total thread is %d"%constant_s#这个结果是3,因为子进程创建按了3个线程 elif (pid >0):#父进程执行到这个if里面 constant_p = constant_s + 1 run(4) time.sleep(5) print "total thread is %d"%constant_s#这个结果也是3,因为该进程也创建了3个线程 else: print "fork error" sys.exit(0) print "total process is %d"%constant_p#该结果是1,因为2个进程只执行了一次 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程序设计有所帮助。
Python相关教程
-
如何Tkinter模块编写Python图形界面
本文讲解为何使用Tkinter而非PyQt,创建一个基本的Tkinter程序,模块化Tkinter程序,希望对大家有帮助。
发布时间:2021-05-02
-
python实现Windows电脑定时关机
这篇文章主要为大家详细介绍了python实现Windows电脑定时关机功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2019-08-26
-
Python数据分析:基于Plotly的动态可视化绘图
大小:191.3 MBPython数据电子书
-
11招玩转网络安全:用Python,更安全
大小:156.5 MB网络安全电子书
-
Python网络爬虫权威指南(第2版)
做为这种收集和了解互联网上海量信息的方法,网页页面爬取技术性变得更加关键。而撰写简易的自动化技术程序流程(网络爬虫),多次就能够全自动爬取几百万个网页页面中的信息内容,
大小:5.54 MBPython爬虫电子书
-
Python数据科学指南
本书是Python数据分析编程入门,详细介绍了Python在数据科学中的应用,60多个实用的开发技巧,帮你探索Python及其强大的数据科学能力
大小:62.3 MBPython数据分析电子书
-
OpenCV算法精解:基于Python与C++
这是一本以OpenCV 为工具学习数字图像处理的入门书,由浅入深的讲解阐述基本概念、数学原理、C++ 实现、Python 实现相结合的方法,适合入门图像处理和计算机视觉领域的初学者阅读
大小:84 MBOpenCV算法电子书
-
Python网络爬虫实战
大小:51MBPython爬虫