python多进程并发
2023-09-11 14:20:41 时间
由于Python下调用Linux的Shell命令都需要等待返回,所以常常我们设置的多线程都达不到效果,
因此在调用shell命令不需要返回时,使用threading模块并不是最好的方法。
http://www.coder4.com/archives/3352
Python提供了非常好用的多进程包multiprocessing,你只需要定义一个函数,Python会替你完成其他所有事情。
借助这个包,可以轻松完成从单进程到并发执行的转换。
1、新建单一进程
如果我们新建少量进程,可以如下:
原文: http://blog.csdn.net/werm520/article/details/43730593
因此在调用shell命令不需要返回时,使用threading模块并不是最好的方法。
http://www.coder4.com/archives/3352
Python提供了非常好用的多进程包multiprocessing,你只需要定义一个函数,Python会替你完成其他所有事情。
借助这个包,可以轻松完成从单进程到并发执行的转换。
1、新建单一进程
如果我们新建少量进程,可以如下:
import multiprocessing import time def func(msg): for i in xrange(3): print msg time.sleep(1) if __name__ == "__main__": p = multiprocessing.Process(target=func, args=("hello", )) p.start() p.join() print "Sub-process done."
2、使用进程池
是的,你没有看错,不是线程池。它可以让你跑满多核CPU,而且使用方法非常简单。
注意要用apply_async,如果落下async,就变成阻塞版本了。
processes=4是最多并发进程数量。
import multiprocessing import time def func(msg): for i in xrange(3): print msg time.sleep(1) if __name__ == "__main__": pool = multiprocessing.Pool(processes=4) for i in xrange(10): msg = "hello %d" %(i) pool.apply_async(func, (msg, )) pool.close() pool.join() print "Sub-process(es) done."
3、使用Pool,并需要关注结果
更多的时候,我们不仅需要多进程执行,还需要关注每个进程的执行结果,如下:
import multiprocessing import time def func(msg): for i in xrange(3): print msg time.sleep(1) return "done " + msg if __name__ == "__main__": pool = multiprocessing.Pool(processes=4) result = [] for i in xrange(10): msg = "hello %d" %(i) result.append(pool.apply_async(func, (msg, ))) pool.close() pool.join() for res in result: print res.get() print "Sub-process(es) done."
根据网友评论中的反馈,在Windows下运行有可能崩溃(开启了一大堆新窗口、进程),可以通过如下调用来解决:
multiprocessing.freeze_support()
附录(自己的脚本):
#!/usr/bin/python import threading import subprocess import datetime import multiprocessing def dd_test(round, th): test_file_arg = 'of=/zbkc/test_mds_crash/1m_%s_%s_{}' %(round, th) command = "seq 100 | xargs -i dd if=/dev/zero %s bs=1M count=1" %test_file_arg print command subprocess.call(command,shell=True,stdout=open('/dev/null','w'),stderr=subprocess.STDOUT) def mds_stat(round): p = subprocess.Popen("zbkc mds stat", shell = True, stdout = subprocess.PIPE) out = p.stdout.readlines() if out[0].find('active') != -1: command = "echo '0205pm %s round mds status OK, %s' >> /round_record" %(round, datetime.datetime.now()) command_2 = "time (ls /zbkc/test_mds_crash/) 2>>/round_record" command_3 = "ls /zbkc/test_mds_crash | wc -l >> /round_record" subprocess.call(command,shell=True) subprocess.call(command_2,shell=True) subprocess.call(command_3,shell=True) return 1 else: command = "echo '0205 %s round mds status abnormal, %s, %s' >> /round_record" %(round, out[0], datetime.datetime.now()) subprocess.call(command,shell=True) return 0 #threads = [] for round in range(1, 1600): pool = multiprocessing.Pool(processes = 10) #使用进程池 for th in range(10): # th_name = "thread-" + str(th) # threads.append(th_name) #添加线程到线程列表 # threading.Thread(target = dd_test, args = (round, th), name = th_name).start() #创建多线程任务 pool.apply_async(dd_test, (round, th)) pool.close() pool.join() #等待线程完成 # for t in threads: # t.join() if mds_stat(round) == 0: subprocess.call("zbkc -s",shell=True) break
相关文章
- python并发编程之进程、线程、协程的调度原理(六)
- python魔法方法之-Python __repr__()方法:显示属性
- Python任意参数的数量/预习课python基础
- Python以进程方式无黑窗在后台运行(无命令窗)
- Python 统计进程池正在执行的进程个数(进程池进程个数)
- python判断进程是否存在
- Google Earth Engine(GEE)——Python 干旱指标PDSI和植被指数NDVI之间的关系
- python中enum枚举类用法详解
- 零基础自学Python需要多长时间从入门到精通?学python能兼职挣钱吗?怎么挣钱?
- Python开发多进程、多线程以及协程是什么?项目是否用?
- 将自己OpenCV-Python-PyCharm开发环境的Python-3.6.8更换为python-3.9.10的详细过程记录
- 129 python高级 - 异步
- 《python 与数据挖掘 》一1.3 Python开发环境的搭建
- python-多种运行方式
- Python根据时间命名并创建文件源码
- python导入模块的过程
- 1.python简介
- Python学习---重点模块之subprocess
- 华为OD机试 - GPU 调度(Python) | 机试题+算法思路+考点+代码解析 【2023】
- 华为OD机试 - 商人买卖(Python) | 机试题+算法思路+考点+代码解析 【2023】
- 华为OD机试 - 检查数组中是否存在满足规则的数组组合1(Python)
- 小学生Python编程 ----- 打鸭子
- Python 常见问题 之 python 安装包下载安装速度慢 的 快速解决方法(之一)
- Python 基础 之 python 协程知识点整理,并实现一个简单 gevent 的协程并发图片下载的应用
- Python 工具 之 Windows 上 python 虚拟环境的搭建与简单使用的相关说明
- (16)python进程、协程、socket(待写)
- Python多进程和多线程对比总结
- 【爬虫系列】【Python】python with as的用法
- 【Python】pyqt5入门教程之第一个UI界面
- Python 取样式的内容 合并多个文件的样式 自定义样式