多进程:主要运行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. mvc 返回一个对象 到视图接收

    public ActionResult InfoFrame() { List<Users> list = new List<Users>(); Users user = new ...

  2. python3 使用pip安装(命令行中)失败或 “not a supported wheel” 解决方案!

    原因1: 安装的不是对应python版本的库,下载的库名中cp36代表python3.6,其它同理. 原因2:(我遇到的情况----下载的是对应版本的库,然后仍然提示不支持当前平台) 百度了一下,说法 ...

  3. 在window系统上安装redis服务-Invalid argument during startup: Failed to open the .conf

    当前redis版本: redis-cli -v redis-cli 3.0.503 网上给的命令(在redis所在文件夹下执行): redis-server –service-install redi ...

  4. MVC中一般为什么用IQueryable而不是用IList?

    IList(IList<T>)会立即在内存里创建持久数据,这就没有实现“延期执行(deferred execution)”,如果被加载的实体有关联实体(associations),此关联实 ...

  5. [转]PuTTY字体颜色设置

    转载于 https://blog.csdn.net/cyd_shuihan/article/details/77836290 用putty登录Linux,默认配色方案看不清,我们可以自己设置新的字体大 ...

  6. Python学习笔记五

    一. 递归 递归函数: def a (): print ("from b") b() def b(): print("from a ") a() a() 递推和 ...

  7. 【EasyNetQ】- 发布/订阅模式

    EasyNetQ支持的最简单的消息传递模式是发布/ 订阅.这种模式是消除消费者信息提供者的绝佳方式.出版商简单地向全世界说,“这已经发生了”或“我现在有了这些信息”.它不关心是否有人正在倾听,他们可能 ...

  8. webpack proxyTable 跨域

    dev-server能在开发环境中生成一个本地服务器来实现代理 在vue-cli下 找的 dev下的 proxyTable: { '/':{ //前缀 target: 'http://www.exam ...

  9. 一起学HBase——简单介绍HBase各种组件

    HBase是谷歌BigTble的开源实现.谷歌的三篇论文拉开了大数据江湖的序幕,铸就了现在以Hadoop为主的大数据技术生态圈.而HBase是开源的大数据数据库,和传统的行式数据库不同的是,HBase ...

  10. SQL反模式学习笔记22 伪键洁癖,整理数据

    目标:整理数据,使不连续的主键Id数据记录变的连续. 反模式:填充断档的数据空缺. 1.不按照顺序分配编号 在插入新行时,通过遍历表,找到的第一个未分配的主键编号分配给新行,来代替原来自动分配的伪主键 ...