Python实现的多进程和多线程功能示例


Posted in Python onMay 29, 2018

本文实例讲述了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处理字符串之isspace()方法的使用
May 19 Python
详解Python发送邮件实例
Jan 10 Python
Python 中开发pattern的string模板(template) 实例详解
Apr 01 Python
Python实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
Aug 07 Python
Python多进程原理与用法分析
Aug 21 Python
python cumsum函数的具体使用
Jul 29 Python
Pandas+Matplotlib 箱式图异常值分析示例
Dec 09 Python
Python Print实现在输出中插入变量的例子
Dec 25 Python
Python切割图片成九宫格的示例代码
Mar 10 Python
python实现音乐播放和下载小程序功能
Apr 26 Python
详解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
Apr 25 Python
Python连续赋值需要注意的一些问题
Jun 03 Python
Python实现的redis分布式锁功能示例
May 29 #Python
Python计算一个给定时间点前一个月和后一个月第一天的方法
May 29 #Python
PyTorch CNN实战之MNIST手写数字识别示例
May 29 #Python
Python根据指定日期计算后n天,前n天是哪一天的方法
May 29 #Python
python 将md5转为16字节的方法
May 29 #Python
python 利用栈和队列模拟递归的过程
May 29 #Python
查看django执行的sql语句及消耗时间的两种方法
May 29 #Python
You might like
国内咖啡文化
2021/03/03 咖啡文化
php中截取字符串支持utf-8
2007/01/18 PHP
php 如何获取数组第一个值
2013/08/06 PHP
thinkPHP中分页用法实例分析
2015/12/26 PHP
PHP扩展Swoole实现实时异步任务队列示例
2019/04/13 PHP
phpwind放自动注册方法
2006/12/02 Javascript
网站如何做到完全不需要jQuery也可以满足简单需求
2013/06/27 Javascript
JavaScript中prototype为对象添加属性的误区介绍
2013/10/15 Javascript
Extjs grid panel自带滚动条失效的解决方法
2014/09/11 Javascript
js实现按钮控制图片360度翻转特效的方法
2015/02/17 Javascript
jQuery.prop() 使用详解
2015/07/19 Javascript
node.js入门教程之querystring模块的使用方法
2017/02/27 Javascript
详解AngularJs路由之Ui-router-resolve(预加载)
2017/06/13 Javascript
js字符限制(字符截取) 一个中文汉字算两个字符
2017/09/12 Javascript
vue-cli实现多页面多路由的示例代码
2018/01/30 Javascript
关于vue面试题汇总
2018/03/20 Javascript
深入理解react-router 路由的实现原理
2018/09/26 Javascript
详解ESLint在Vue中的使用小结
2018/10/15 Javascript
vue-cli+iview项目打包上线之后图标不显示问题及解决方法
2019/10/16 Javascript
js实现烟花特效
2020/03/02 Javascript
nuxt.js添加环境变量,区分项目打包环境操作
2020/11/06 Javascript
一篇文章让你搞懂JavaScript 原型和原型链
2020/11/23 Javascript
在Linux下使用Python的matplotlib绘制数据图的教程
2015/06/11 Python
Python安装lz4-0.10.1遇到的坑
2018/05/20 Python
python绘制热力图heatmap
2020/03/23 Python
Python 识别12306图片验证码物品的实现示例
2020/01/20 Python
Python搭建Keras CNN模型破解网站验证码的实现
2020/04/07 Python
pycharm 实现本地写代码,服务器运行的操作
2020/06/08 Python
详解用selenium来下载小姐姐图片并保存
2021/01/26 Python
澳大利亚冒险体验:Adrenaline(跳伞、V8赛车、热气球等)
2017/09/18 全球购物
巴黎欧莱雅法国官网:L’Oreal Paris
2019/04/30 全球购物
工作疏忽检讨书500字
2014/10/26 职场文书
优秀教师个人材料
2014/12/15 职场文书
客户答谢会致辞
2015/07/30 职场文书
学习雷锋主题班会
2015/08/14 职场文书
PHP设计模式(观察者模式)
2021/07/07 PHP