学习完线程,学习进程

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

模块是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()

Python学习-day10 进程的更多相关文章

  1. Python学习--17 进程和线程

    线程是最小的执行单元,而进程由至少一个线程组成.如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间. 进程 fork调用 通过fork()系统调用,就可以生成一个子进程 ...

  2. Python学习--18 进程和线程

    线程是最小的执行单元,而进程由至少一个线程组成.如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间. 进程 fork调用 通过fork()系统调用,就可以生成一个子进程 ...

  3. Python Revisited Day10 (进程与线程)

    目录 10.1 使用多进程模块 10.2 将工作分布到多个线程 <Python 3 程序开发指南>学习笔记 有俩种方法可以对工作载荷进行分布,一种是使用多进程,另一种是使用多线程. 10. ...

  4. python学习笔记-进程线程

    1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...

  5. python学习Day10 函数的介绍(定义、组成、使用)

    今日学习内容: 1.什么是函数 :函数就是一个含有特定功能的变量,一个解决某问题的工具 函数的定义:通过关键字def + 功能名字():代码体(根据需求撰写代码逻辑) 2.为什么要用函数:可以复用:函 ...

  6. python学习(十三)进程和线程

    python多进程 from multiprocessing import Process import os def processFunc(name): print("child pro ...

  7. Python学习 day10

    一.默认参数的陷阱 先看如下例子: def func(li=[]): li.append(1) print(li) func() func() func(li=['abc']) func() 结果: ...

  8. python 学习分享-进程

    python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包multiprocessing,只需要定 ...

  9. python学习之-- 进程 和 线程

    python 进程/线程详解 进程定义:以一个整体的形式暴露给操作系统管理,它里面包含对各种资源的调用,内存的管理,网络接口的调用等等,对各种资源管理的集合,就可以叫做一个进程. 线程定义:线程是操作 ...

随机推荐

  1. 编译错误you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)

    解决方法: export FORCE_UNSAFE_CONFIGURE=1

  2. JAVA程序员必须要学习的知识

    Java是热门的语言之一,TIOBE编程语排名Java排名第二,仅在C语言之后.Java可以用来开发web应用和桌面应用,更重要的是Java具有跨平台性:write once, run everywh ...

  3. 谷歌浏览器Chrome developer tool详细介绍

    http://www.cr173.com/html/19114_4.html 第 4 页 js调试源码控制面板 5 源码控制面板(js调试) Javascript的调试,基本上是通过源码控制面板和命令 ...

  4. 如何用JavaScript实现2+2=5?

    我大学毕业找工作时,经常做一些稀奇古怪的面试题.这不,给大家分享一道整蛊的面试题,它其实不能算一道正式的面试题,大家可以用它来捉弄你们那些程序员朋友. 题目:如何用JavaScript实现2+2=5? ...

  5. Unity中的各种寻找GameObject方法归纳

    1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象: 路径如官方事例写法: public class ExampleClass : Mono ...

  6. opencv c++编译

    g++ image2png.cpp -o test `pkg-config --cflags --libs opencv`

  7. Python 继承实现的原理(继承顺序)

    继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...

  8. QT5:第一章 初始化

    一.简介 二.新建项目 在项目Application中: QT Widgets Application(桌面QT应用) QT Console Application(控制台QT应用) QT for P ...

  9. atoi 函数实现

      要考虑的东西实在也挺多的.总结如下:   1 前面空格分隔符号的时候   2 第一个符号位处理+ -   3 遇到非数字字符退出   4 为正数的时候,大于INT_MAX上溢   5 为负数的时候 ...

  10. 掉坑日志:Windows Native API与DPI缩放

    高DPI显示器越来越普及,软件自然也要适应这个变化,最近实习的时候也遇到了一个关于DPI缩放的问题.因为内部框架的一个控件有BUG,会导致内容的显示出问题,后来实在没办法改成了用Windows Nat ...