import time
from multiprocessing import Process,Pool
def f1(n):
time.sleep(0.5)
# print(n)
return n*n
if __name__ == '__main__':
pool = Pool(4)
# pool.apply(f1,args=(2,)) #同步方法
res_list = []
for i in range(10):
# print('任务%s'%i)
#进程池的同步方法,将任务变成了串行
# res = pool.apply(f1,args=(i,))
# print(res)
#进程池的异步方法
res = pool.apply_async(f1,args=(i,))
print(res)
# as_result = res.get() #join的效果
# print(as_result)
res_list.append(res)
pool.close() #锁住进程池,不再让其他的程序往里面扔新的任务了,确保没有新的任务交给进程池里面的进程
pool.join()
for r in res_list:
print(r.get())
time.sleep(2)
#主进程运行结束,进程池里面的任务全部停止,不会等待进程池里面的任务
print('主进程直接结束')
p = Process(target=f1,)

Python 进程池的同步方法和异步方法的更多相关文章

  1. Python 进程池的同步方法

    import time from multiprocessing import Process,Pool def f1(n): time.sleep(1) #print(n) return n*n i ...

  2. Python 进程池的异步方法

    import time from multiprocessing import Process,Pool def f1(n): time.sleep(0.5) # print(n) return n* ...

  3. python进程池:multiprocessing.pool

    本文转至http://www.cnblogs.com/kaituorensheng/p/4465768.html,在其基础上进行了一些小小改动. 在利用Python进行系统管理的时候,特别是同时操作多 ...

  4. python(进程池/线程池)

    进程池 import multiprocessing import time def do_calculation(data): print(multiprocessing.current_proce ...

  5. python进程池剖析(三)

    之前文章对python中进程池的原理.数据流以及应用从代码角度做了简单的剖析,现在让我们回头看看标准库中对进程池的实现都有哪些值得我们学习的地方.我们知道,进程池内部由多个线程互相协作,向客户端提供可 ...

  6. python进程池剖析(二)

    之前文章中介绍了python中multiprocessing模块中自带的进程池Pool,并对进程池中的数据结构和各个线程之间的合作关系进行了简单分析,这节来看下客户端如何对向进程池分配任务,并获取结果 ...

  7. python进程池剖析(一)

    python中两个常用来处理进程的模块分别是subprocess和multiprocessing,其中subprocess通常用于执行外部程序,比如一些第三方应用程序,而不是Python程序.如果需要 ...

  8. 万里长征第一步:Python进程池的一点点小坑

    # -*- coding: utf- -*- """ Created on Thu Mar :: @author: lilide """ # ...

  9. python进程池

    当需要创建的子进程数量不多时,可以直接利用multiprocessing中的Process动态成生多个进程,但如果是上百甚至上千个目标,手动的去创建进程的工作量巨大,此时就可以用到multiproce ...

随机推荐

  1. win10忘记开机密码无法进入桌面

    第一种: 电脑用微软账户登录,但密码始终不正确. 登陆这个网址    https://account.live.com/password/reset 按照提示的操作利用之前注册信息一步步重设密码 最后 ...

  2. Navigation包中的move_base和amcl实现自动驾驶

    安装功能包: 1.安装导航定位包navigation $ sudo apt-get install ros-indigo-navigation 2.由于导航包在/cmd_val下发布的移动数据加速度会 ...

  3. Docker Overlay 应用部署

    Docker Overlay 部署条件 要想使用Docker原生Overlay网络,需要满足以下任意条件: 1.Docker运行在Swarm模式 2.使用键值存储的Docker主机集群 本次部署使用键 ...

  4. [译]课程 1: 使用 Quartz

    译者注: 原文在这 Lesson 1: Using Quartz 在你使用调度器之前, 你需要先实例化(能猜到是谁么?). 要实例化, 请使用 ISchedulerFactory 的实现. 译者注: ...

  5. IDEA的校园邮箱激活方式

    链接: https://blog.csdn.net/m0_37286282/article/details/78279060

  6. bind封装

    原理:通过apply或者call方法来实现. (1)初始版本 Function.prototype.bind=function(obj,arg){ var arg=Array.prototype.sl ...

  7. [CodeForces - 463B] Caisa and Pylons

    题目链接:http://codeforces.com/problemset/problem/463/B 求个最大值 AC代码: #include<cstdio> #include<c ...

  8. 【SQL Server 问题记录】A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

    本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 A network-related or instance-specific error occurred while esta ...

  9. ffmpeg 图像转视频 视频转图像

    ffmpeg使用 以下两条可使用,具体可参考:https://blog.csdn.net/pkueecser/article/details/8555261pic to video:ffmpeg -f ...

  10. SpringBoot MyBatis 配置多数据源 (静态多个)

    转载地址:https://www.jianshu.com/p/118ca1d5ecf9?utm_campaign=haruki&utm_content=note&utm_medium= ...