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 条件判断的缩写方法
Sep 06 Python
Python编码时应该注意的几个情况
Mar 04 Python
Python 用Redis简单实现分布式爬虫的方法
Nov 23 Python
删除python pandas.DataFrame 的多重index实例
Jun 08 Python
python数据批量写入ScrolledText的优化方法
Oct 11 Python
Python对象与引用的介绍
Jan 24 Python
Django框架使用mysql视图操作示例
May 15 Python
Python程序包的构建和发布过程示例详解
Jun 09 Python
Pycharm最新激活码2019(推荐)
Dec 31 Python
python实现贪吃蛇游戏源码
Mar 21 Python
Pycharm同步远程服务器调试的方法步骤
Nov 04 Python
python如何用matplotlib创建三维图表
Jan 26 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
用PHP和ACCESS写聊天室(六)
2006/10/09 PHP
使用迭代器 遍历文件信息的详解
2013/06/08 PHP
php全角字符转换为半角函数
2014/02/07 PHP
php中Ctype函数用法详解
2014/12/09 PHP
PHP对XML内容进行修改和删除实例代码
2016/10/26 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
PHP解析url并得到url参数方法总结
2018/10/11 PHP
浅谈PHP5.6 与 PHP7.0 区别
2019/10/09 PHP
js加强的经典分页实例
2013/03/15 Javascript
javascript history对象(历史记录)使用方法(实现浏览器前进后退)
2014/01/07 Javascript
jQuery实现下拉框左右移动(全部移动,已选移动)
2016/04/15 Javascript
node.js express安装及示例网站搭建方法(分享)
2016/08/22 Javascript
微信小程序 Audio API详解及实例代码
2016/09/30 Javascript
Bootstrap 实现查询的完美方法
2016/10/26 Javascript
浅谈Fetch 数据交互方式
2018/12/20 Javascript
JavaScript获取某一天所在的星期
2019/09/05 Javascript
基于html+css+js实现简易计算器代码实例
2020/02/28 Javascript
js构造函数constructor和原型prototype原理与用法实例分析
2020/03/02 Javascript
es6中new.target的作用和使用场景简单示例分析
2020/03/14 Javascript
vue-admin-template配置快捷导航的代码(标签导航栏)
2020/09/04 Javascript
使用pip发布Python程序的方法步骤
2018/10/11 Python
利用Python的turtle库绘制玫瑰教程
2019/11/23 Python
python 的topk算法实例
2020/04/02 Python
浅谈keras.callbacks设置模型保存策略
2020/06/18 Python
CSS3 绘制BMW logo实的现代码
2013/04/25 HTML / CSS
二年级体育教学反思
2014/01/15 职场文书
采购助理岗位职责
2014/02/16 职场文书
校庆筹备方案
2014/03/30 职场文书
春游踏青活动方案
2014/08/14 职场文书
2014小学生国庆65周年演讲稿
2014/09/21 职场文书
2014年校长工作总结
2014/12/11 职场文书
家属答谢词
2015/01/05 职场文书
莫言诺贝尔获奖感言(全文)
2015/07/31 职场文书
nginx优化的六点方法
2021/03/31 Servers
MySQL配置主从服务器(一主多从)
2021/08/07 MySQL
Android中View.post和Handler.post的关系
2022/06/05 Java/Android