多进程:主要运行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多进程使用及线程池的使用方法的更多相关文章

  1. 《转载》Python并发编程之线程池/进程池--concurrent.futures模块

    本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...

  2. python自带的线程池和进程池

    #python自带的线程池 from multiprocessing.pool import ThreadPool #注意ThreadPool不在threading模块下 from multiproc ...

  3. java中线程池的使用方法

    1 引入线程池的原因 由于线程的生命周期中包括创建.就绪.运行.阻塞.销毁阶段,当我们待处理的任务数目较小时,我们可以自己创建几个线程来处理相应的任务,但当有大量的任务时,由于创建.销毁线程需要很大的 ...

  4. 13.ThreadPoolExecutor线程池之submit方法

    jdk1.7.0_79  在上一篇<ThreadPoolExecutor线程池原理及其execute方法>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法 ...

  5. python(13)线程池:threading

    先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequests(get_title, da ...

  6. Python并发编程之线程池&进程池

    引用 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/销毁进程或者线程是非常消耗资源的,这个时候我 ...

  7. Python并发编程之线程池/进程池--concurrent.futures模块

    一.关于concurrent.futures模块 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/ ...

  8. python——有一种线程池叫做自己写的线程池

    这周的作业是写一个线程池,python的线程一直被称为鸡肋,所以它也没有亲生的线程池,但是竟然被我发现了野生的线程池,简直不能更幸运~~~于是,我开始啃源码,实在是虐心,在啃源码的过程中,我简略的了解 ...

  9. python 收录集中实现线程池的方法

    概念: 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就创 ...

随机推荐

  1. 使用Jenkins docker镜像运行Jenkins服务

    需求 使用docker技术管理Jenkins服务器.避免多次部署需要重复安装的重复工作,且可以方便迁移到新的服务器. Jenkins docker镜像 https://hub.docker.com/_ ...

  2. react中键盘enter事件处理

    对于常见的搜索需求业务场景,用户输入完成后,点击enter事件请求数据,要求不提交页面,实现数据局部更新,这需要用到react中的表单Forms. 处理方法: (1)html书写 form标签中去掉a ...

  3. Nginx--服务部署、基于域名的虚拟主机配置

    一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...

  4. wget无法正确下载jdk解决方案

    1 去官网复制下载链接 https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/ ...

  5. JS中小数相加相减时出现很长的小数点的解决方式

    1.问题: 平时写的代码中会出现这种情况,parseFloat(11.3-10.1) 运行的结果依然是1.200000000000001 代码示例: var arr = [0.0111,11.002, ...

  6. C# - Visual Studio简明操作

    Visual Studio简明操作 安装Northwind示例数据库 运行安装程序,结束安装后,再CMD中输入以下命令 cd C:\SQL Server  Sample Databases(回车) s ...

  7. Bootstrap模态框钩子事件

    事件类型   描述show.bs.modal show 方法调用之后立即触发该事件.如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问.shown.b ...

  8. Jumpserver(堡垒机)的安装与应用

    官网:http://docs.jumpserver.org/zh/docs/introduce.html 作者:邓聪聪 环境 系统: CentOS 7.6 IP: 172.16.16.2 关闭 sel ...

  9. Linux下安装VMware Tools(使虚拟机支持文件拖拽)

    如图点击虚拟机找到安装VMware Tools选项,点击后会在虚拟机桌面显示一个光盘,双击进入如下页面: 选择压缩包将其复制放入Home中不带中文的文件夹: 打开终端,输入cd命令进入文件夹,将压缩包 ...

  10. TF-tf.arg_max 介绍

    定义为 def arg_max(input, dimension, name=None) 作用是取行或者列的最大值的位置. input:类型为 float32, float64, int64, int ...