学习完线程,学习进程

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

模块是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. IOS使用固定定位遇到的问题

    近日需要实现移动端页面额外功能按钮,即点击加号弹出点赞与留言功能,通常这个按钮都会固定于页面的右下角,首先就想到使用固定定位来实现. 但是在测试时我们发现,在IOS中,当系统键盘弹出时,fixed会失 ...

  2. android动画ppt整理

    案例

  3. 数据库操作----找了MySQL和SQL Sever两个的基础语句

    这是MySQL的基本操作: 1 登入数据库:mysql -uroot -p+密码 (SQL Sever登入: osql -U 用户名 -P 密码) 显示已存在的数据库:show databases; ...

  4. python(一)

    1 python安装 先安装python,之后安装pycharm 创建工程时需要关联解释器路径: 2 python运行 先编译后解释 .py--.pyc文件---解析----结果 .pyc的目的是减少 ...

  5. Win10微软帐户切换不回Administrator本地帐户的解决方法【亲测】

    在Win10系统中经常会用到微软帐户登录,如应用商店等地方,不过一些用户反馈原来使用Administrator帐户被绑定微软帐户后无法切换回本地帐户,连[改用本地帐户登录]按钮都没有,那么怎么解决呢? ...

  6. 怎么在WEBSTORM中设置代码模板 Live Templates

    怎么在WEBSTORM中设置代码模板 Live Templates setting 里面 https://www.cnblogs.com/xinzaimengzai/p/9938464.html

  7. Collatz Conjecture

    4981: Collatz Conjecture 时间限制: 6 Sec  内存限制: 128 MB提交: 213  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 In 19 ...

  8. linux 下使用 curl 访问带多参数,GET掉参数解决方案

    url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用 $_GET是可以获取到所有的参数 curl  -s  ...

  9. FMDB中的数据处理

    [self.db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)" ...

  10. vc文件操作汇总—支持wince

    一.判断文件及文件夹是否存在 // 判断文件是否存在 BOOL IsFileExist(const CString& csFile) { DWORD dwAttrib = GetFileAtt ...