Python学习-day10 进程
学习完线程,学习进程
进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大。
模块是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 进程的更多相关文章
- Python学习--17 进程和线程
线程是最小的执行单元,而进程由至少一个线程组成.如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间. 进程 fork调用 通过fork()系统调用,就可以生成一个子进程 ...
- Python学习--18 进程和线程
线程是最小的执行单元,而进程由至少一个线程组成.如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间. 进程 fork调用 通过fork()系统调用,就可以生成一个子进程 ...
- Python Revisited Day10 (进程与线程)
目录 10.1 使用多进程模块 10.2 将工作分布到多个线程 <Python 3 程序开发指南>学习笔记 有俩种方法可以对工作载荷进行分布,一种是使用多进程,另一种是使用多线程. 10. ...
- python学习笔记-进程线程
1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...
- python学习Day10 函数的介绍(定义、组成、使用)
今日学习内容: 1.什么是函数 :函数就是一个含有特定功能的变量,一个解决某问题的工具 函数的定义:通过关键字def + 功能名字():代码体(根据需求撰写代码逻辑) 2.为什么要用函数:可以复用:函 ...
- python学习(十三)进程和线程
python多进程 from multiprocessing import Process import os def processFunc(name): print("child pro ...
- Python学习 day10
一.默认参数的陷阱 先看如下例子: def func(li=[]): li.append(1) print(li) func() func() func(li=['abc']) func() 结果: ...
- python 学习分享-进程
python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包multiprocessing,只需要定 ...
- python学习之-- 进程 和 线程
python 进程/线程详解 进程定义:以一个整体的形式暴露给操作系统管理,它里面包含对各种资源的调用,内存的管理,网络接口的调用等等,对各种资源管理的集合,就可以叫做一个进程. 线程定义:线程是操作 ...
随机推荐
- js读取excel数据后的时间格式转换
使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ...
- JavaScript空假值及其判断
一.javaScript五种空值和假值 分别为undefined,null,false,"",0,这五个值的共同点是在执行if语句时都会执行false分支,执行对应的非语句的时候都 ...
- Windows系统命令行下编译连接C/C++源代码方法
Windows系统下编译连接源代码方法:cl -GX test.c-GX: 启动同步异常处理上面的命令会产生可执行程序:test.exe在命令行中直接输入:test.exe 就可运行该程序 Tips: ...
- POJ Charm Bracelet 挑饰品 (常规01背包)
问题:去珠宝店抢饰品,给出饰品种数n,能带走的重量m,以及每种饰品的重量w与价值v.求能带走的最大量. 思路:常规01背包. #include <iostream> using names ...
- UWP开发:存储容器设置&复合设置数据
有时候为了将应用设置进行分类,需要创建新的容器进行存储应用设置的信息. 1,容器的创建:在一个根容器里嵌套一个新容器 1)首先获取根容器. 2)调用ApplicationDataContainer.C ...
- [论文理解]SSD:Single Shot MultiBox Detector
SSD:Single Shot MultiBox Detector Intro SSD是一套one-stage算法实现目标检测的框架,速度很快,在当时速度超过了yolo,精度也可以达到two-stag ...
- python中os.listdir( )函数读取文件夹
编写pytohn脚本时通常需要批处理. 列出指定目录下的所有文件/文件夹 os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表,但有个很明显的缺点,它的默认顺序不是有序的或 ...
- oracle 快速复制一张表,并在此创建索引,日志及并行度
复制表结构及其数据 create table table_name_new as select * from table_name_old 只复制表结构 create table table_name ...
- 20181111 计时器影响DOM点击事件的逻辑
今天在群里看见一个人在问"点击按钮使图片产生旋转为什么要使用计时器来实现",我自己操作了一遍她的代码才发现里面的逻辑实现很有意思,所以写出来分享一下. 她的代码是这样写的: < ...
- centos6启动故障排除
centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...