python多进程使用及线程池的使用方法代码详解


Posted in Python onOctober 24, 2018

多进程:主要运行multiprocessing模块

import os,time
import sys
from multiprocessing import Process
class MyProcess(Process):
  """docstring for MyProcess"""
  def __init__(self, arg, callback):
    super(MyProcess, self).__init__()
    self.arg = arg
    self.callback = callback
  def run(self):
    self.callback(self.arg)
def test(arg):
  print("子进程{}开始>>> pid={}".format(arg,os.getpid()))
  for i in range(1,5):
    sys.stdout.write("子进程{}运行中{}\r".format(arg,i))
    sys.stdout.flush()
    time.sleep(1)
def main():
  print("主进程开始>>> pid={}".format(os.getpid()))
  myp=MyProcess(1,test)
  myp.start()
  myp2=MyProcess(2,test)
  myp2.start()
  myp.join()
  myp2.join()
  print("主进程终止")
if __name__ == '__main__':
  main()

线程池:主要运用了未来模块!下面例子,第一个是正常,第二第线程池,第三个用运行了2个线程池,会排队

from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a):
  print("hello: "+a)
  time.sleep(2)
def main():
  seed=["a","b","c"]
  start1=time.time()
  for each in seed:
    sayhello(each)
  end1=time.time()
  print("time1: "+str(end1-start1))
  start2=time.time()
  with ThreadPoolExecutor(3) as executor:
    for each in seed:
      executor.submit(sayhello,each)
  end2=time.time()
  print("time2: "+str(end2-start2))
  start3=time.time()
  with ThreadPoolExecutor(2) as executor1:
    executor1.map(sayhello,seed)
  end3=time.time()
  print("time3: "+str(end3-start3))
if __name__ == '__main__':
  main()

总结

以上所述是小编给大家介绍的python多进程使用及线程池的使用方法代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python 从远程服务器下载东西的代码
Feb 10 Python
Python中subprocess的简单使用示例
Jul 28 Python
python利用lxml读写xml格式的文件
Aug 10 Python
Python自动化运维之IP地址处理模块详解
Dec 10 Python
python爬虫获取京东手机图片的图文教程
Dec 29 Python
Python实现获取nginx服务器ip及流量统计信息功能示例
May 18 Python
python编写简易聊天室实现局域网内聊天功能
Jul 28 Python
python中提高pip install速度
Feb 14 Python
python和php哪个容易学
Jun 19 Python
教你用python控制安卓手机
May 13 Python
pandas提升计算效率的一些方法汇总
May 30 Python
Python上下文管理器Content Manager
Jun 26 Python
python实现图片筛选程序
Oct 24 #Python
用pycharm开发django项目示例代码
Oct 24 #Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
Oct 23 #Python
Python读取mat文件,并保存为pickle格式的方法
Oct 23 #Python
Python读取系统文件夹内所有文件并统计数量的方法
Oct 23 #Python
Python实现按逗号分隔列表的方法
Oct 23 #Python
Python解析Excle文件中的数据方法
Oct 23 #Python
You might like
php Undefined index和Undefined variable的解决方法
2008/03/27 PHP
Yii框架form表单用法实例
2014/12/04 PHP
JavaScript的面向对象(一)
2006/11/09 Javascript
JavaScript实现横向滑出的多级菜单效果
2015/10/09 Javascript
jquery判断当前浏览器的实现代码
2015/11/07 Javascript
Google 爬虫如何抓取 JavaScript 的内容
2017/04/07 Javascript
JavaScript实现form表单的多文件上传
2020/03/27 Javascript
详解用webpack把我们的业务模块分开打包的方法
2017/07/20 Javascript
JavaScript数据结构之栈实例用法
2019/01/18 Javascript
js字符串类型String常用操作实例总结
2019/07/05 Javascript
JS实现利用闭包判断Dom元素和滚动条的方向示例
2019/08/26 Javascript
vue输入框使用模糊搜索功能的实现代码
2020/05/26 Javascript
Python中的sort()方法使用基础教程
2017/01/08 Python
对python .txt文件读取及数据处理方法总结
2018/04/23 Python
Numpy中的mask的使用
2018/07/21 Python
python判断计算机是否有网络连接的实例
2018/12/15 Python
Python 按字典dict的键排序,并取出相应的键值放于list中的实例
2019/02/12 Python
浅谈Python反射 & 单例模式
2019/03/21 Python
pymongo中group by的操作方法教程
2019/03/22 Python
Series和DataFrame使用简单入门
2019/11/13 Python
Python使用itcaht库实现微信自动收发消息功能
2020/07/13 Python
使用tensorflow进行音乐类型的分类
2020/08/14 Python
如何用python批量调整视频声音
2020/12/22 Python
为你的html5网页添加音效示例
2014/04/03 HTML / CSS
Giuseppe Zanotti美国官方网站:将鞋履视为高级时装般精心制作
2018/02/06 全球购物
瑞士灯具购物网站:Lampenwelt.ch
2018/07/08 全球购物
餐饮业会计岗位职责
2013/12/19 职场文书
竞选体育委员演讲稿
2014/04/26 职场文书
项目经理岗位职责
2015/01/31 职场文书
2015年助残日活动总结
2015/03/27 职场文书
主持人开场白台词
2015/05/29 职场文书
仰望星空观后感
2015/06/10 职场文书
钢铁是怎样炼成的读书笔记
2015/06/29 职场文书
2019开业庆典剪彩仪式主持词!
2019/07/22 职场文书
python 判断文件或文件夹是否存在
2022/03/18 Python
SQL Server数据库查询出现阻塞之性能调优
2022/04/10 SQL Server