学习完线程,学习进程

进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大。

模块是threading 和 multiprocessing

多进程multiprocessing

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

如何启动多进程

#Authon Ivor
from multiprocessing import Process
import os def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
print("") def f(name):
info('\033[31;1mfunction f\033[0m')
print('hello', name) if __name__ == '__main__':
info('\033[32;1mmain process line\033[0m')
p = Process(target=f, args=('bob',))
p.start()
p.join()

进程间通信的三种方式

Queue

#Author:Ivor
from multiprocessing import Process,Queue
import os
# threading.queue.Queue()
def run(q):
q.put("---in the Child process---")
print("parent_pid:",os.getppid())
print("current_pid:",os.getpid())
print("------------------") if __name__ == '__main__':
q = Queue()
print("---main process---")
print("parent_pid:",os.getppid())
print("current_pid:",os.getpid())
print("------------------")
p = Process(target=run,args=(q,))
p.start()
print(q.get())
p.join()

Pipe

#Author:Ivor
from multiprocessing import Process,Pipe def run(conn):
conn.send("from child")
conn.close() if __name__ == '__main__':
parent_conn,child_conn = Pipe()
p = Process(target=run,args=(child_conn,))
p.start()
print(parent_conn.recv())
p.join()

Manager

#Author:Ivor
from multiprocessing import Process,Manager
import os
def run(d,l):
d[os.getpid()] = os.getpid()
l.append(os.getpid())
print(l) pro_list = []
if __name__ == '__main__':
manager = Manager()
d = manager.dict()
l = manager.list()
for i in range(10):
p = Process(target=run,args=(d,l))
pro_list.append(p)
p.start()
for i in pro_list:
i.join()
print(d)
print(l)

进程池的概念

Pool

#Authon Ivor
from multiprocessing import Process,Pool
import time,os
def run(n):
print("Process %s is running.." % n)
time.sleep(1)
return os.getpid() def bar(arg):
print("exec done---",arg) result = []
if __name__ == '__main__':
pool = Pool(processes=2)
for n in range(10):
result.append(pool.apply_async(func=run,args=(n,),callback=bar))
for res in result:
print("res---",res.get())
pool.close()
pool.join()

最新文章

  1. [转]Win2003打不开https的问题
  2. 盘点mysql中容易被我们误会的地方
  3. 配置和使用buffer cache
  4. (博弈论)hdoj 1525 Euclid's Game
  5. mac上java开发环境
  6. 客户端javascript笔记
  7. spring Aop 注解
  8. Dx 1 error; aborting Conversion to Dalvik format failed with error 1
  9. 字符和字符串处理-ANSI字符和Unicode字符
  10. Navicat for mysql 导出sql文件 导入sql文件
  11. extern、static、auto、register 定义变量的不同用法
  12. sqlserver缓存程序-只能使用一次清除缓存计划
  13. 【css3网页布局】flex盒子模型
  14. gradle 修改生成的apk的名字
  15. x86/x64/x86_64/i386/ia32/ia64/amd/amd64 辨析
  16. springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
  17. myeclipse16怎么去掉项目中的CodeLive Panel?
  18. 基于ARM Cortex-M0+ 的Bootloader 参考
  19. APP支付-》支付宝RSA2->支付与验签
  20. unity常用小知识点

热门文章

  1. Linux 网卡驱动的安装
  2. Android 使用RecyclerView实现多行水平分页的GridView效果和ViewPager效果
  3. Monitorix系统和网络监控工具
  4. 单列表变量与字符串拆分的对照(SqlServer)
  5. 拷贝文件至U盘——提示:对于目标系统文件过大
  6. 洛谷 P2383 狗哥玩木棒
  7. xcode技巧
  8. python基础教程总结15——6 CGI远程编辑
  9. 将Java应用部署到SAP云平台neo环境的两种方式
  10. CentOS配置主机名和主机映射