python多进程使用及线程池的使用方法
多进程:主要运行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并发编程之线程池/进程池--concurrent.futures模块
本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...
- python自带的线程池和进程池
#python自带的线程池 from multiprocessing.pool import ThreadPool #注意ThreadPool不在threading模块下 from multiproc ...
- java中线程池的使用方法
1 引入线程池的原因 由于线程的生命周期中包括创建.就绪.运行.阻塞.销毁阶段,当我们待处理的任务数目较小时,我们可以自己创建几个线程来处理相应的任务,但当有大量的任务时,由于创建.销毁线程需要很大的 ...
- 13.ThreadPoolExecutor线程池之submit方法
jdk1.7.0_79 在上一篇<ThreadPoolExecutor线程池原理及其execute方法>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法 ...
- python(13)线程池:threading
先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequests(get_title, da ...
- Python并发编程之线程池&进程池
引用 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/销毁进程或者线程是非常消耗资源的,这个时候我 ...
- Python并发编程之线程池/进程池--concurrent.futures模块
一.关于concurrent.futures模块 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/ ...
- python——有一种线程池叫做自己写的线程池
这周的作业是写一个线程池,python的线程一直被称为鸡肋,所以它也没有亲生的线程池,但是竟然被我发现了野生的线程池,简直不能更幸运~~~于是,我开始啃源码,实在是虐心,在啃源码的过程中,我简略的了解 ...
- python 收录集中实现线程池的方法
概念: 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就创 ...
随机推荐
- win10下maven的安装与配置
下载apache-maven-3.5.3-bin.zip并解压: 环境配置: 系统环境:添加=>MAVEN_HOME 值为apache-maven-3.5.3的路径(D:\xxx\apache- ...
- ASP.NET Web API系列教程(目录)(转)
注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP.NET Web API.这是一个用来在.NET平台上建立HTTP服务的Web API框架,是微软的又一项令人振奋的技术.目前,国内 ...
- sed追加文本-sed脚本追加文本
input为sed输入文件,内容如下: [root@node1 gitlab-test-]# cat inppu.txt aa bb cc dd 追加文本: 1.匹配 aa 行之后追加文本 We a ...
- typecho视频播放插件JWPlayer
JWplayer for typecho是羽中大神开发并持续维护的一款插件,目前插件已经发布了8个版本,涵盖typecho0.8到1.0,插件基于原生的JWPlayer,可以说非常完美,详细使用方法在 ...
- ubuntu 16.04 安装 kubelet、kubeadm 和 kubectl
解决了***之后,就开始K8S安装的正式旅程,本次记录 kubelet.kubeadm 和 kubectl 的安装: apt-get update && apt-get instal ...
- 20175226 2018-2019-2 《Java程序设计》第四周学习总结
20175226 2018-2019-2 <Java程序设计>第四周学习总结 教材学习内容总结 子类与父类 格式class 子类名 extends 父类名 Object类是所有类的祖先类 ...
- win10 激活工具 Re-LoaderByR@1n.exe
名字叫 Re-LoaderByR@1n.exe 无广告, https://pan.baidu.com/s/1MBix1cZxKpUTA6MUgL7uLQ 如果变成教育版,可以用以下激活码变成专业版:W ...
- 根据ip获取对应的省市区
public static String getAddressByIp(String ip) { String resout = ""; try { if (isInner(ip) ...
- python3 字典(dictionary)(一)
一.定义:是另一种可变容器模型,可存储任意类型对象:(也被称为关联数组或哈希表:存储的数据是没有顺序的) 语法为: d = {key1 : value1, key2 : value2 } #----- ...
- python 列表 元组 字符串
列表添加: list.append() list.extend() list.insert() 列表删除: list.remove() #删除某一个元素 list.pop() #删除某一个返回删 ...