Python的并发并行[3] -> 进程[1] -> 多进程的基本使用
多进程的基本使用
1 subprocess 常用函数示例
首先定义一个子进程调用的程序,用于打印一个输出语句,并获取命令行参数
import sys
print('Called_Function.py called, Hello world.')
try:
print('Got para', sys.argv[1:])
except:
pass
再定义主函数,即父进程,分别测试 run() / call() / check_call() / getstatusoutput() / getoutput() / ckeck_output函数。
import subprocess # subprocess.run()
print('---------subprocess.run------------')
re = subprocess.run(['python', 'Called_Function.py', 'para_1', 'para_2'])
print('subprocess.run() test returns obj:', re)
print('Return code is: {0}, stdout is: {1}, stderr is: {2}'.format(re.returncode, re.stdout, re.stderr)) # subprocess.call()
print('\n---------subprocess.call------------')
print('subprocess.call() test returns code:', subprocess.call(['python', 'Called_Function.py', 'para_1', 'para_2'])) # subprocess.ckeck_call()
print('\n---------subprocess.check_call------------')
try:
print('subprocess.check_call() test returns code:', subprocess.check_call(['python', 'Called_Function.py']))
except subprocess.CalledProcessError:
print('Failed to call.') # subprocess.getstatusoutput()
print('\n---------subprocess.getstatusoutput------------')
print('subprocess.getstatusoutput() test returns:', subprocess.getstatusoutput(['python', 'Called_Function.py'])) # subprocess.getoutput()
print('\n---------subprocess.getstatusoutput------------')
print('subprocess.getoutput() test returns:', subprocess.getoutput(['python', 'Called_Function.py'])) # subprocess.check_output()
print('\n---------subprocess.check_output------------')
print('subprocess.check_output() test returns:', subprocess.check_output(['python', 'Called_Function.py']))
2 利用Popen类与子进程交互
首先定义一个子进程调用的函数,函数中需求一个输入
x = input('Please input something.')
print(x, 'Hello World!')
# Raise an error
print(y)
再定义父进程的函数
import subprocess prcs = subprocess.Popen(['python', 'Called_Function_Popen.py'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=True) print('subprocess pid:', prcs.pid) re = prcs.communicate('These string are from stdin')
print('\nSTDOUT:', re[0])
print('\nSTDERR:', re[1])
if prcs.poll():
print('\nThe subprocess has been done')
相关阅读
Python的并发并行[3] -> 进程[1] -> 多进程的基本使用的更多相关文章
- Python的并发并行[3] -> 进程[0] -> subprocess 模块
subprocess 模块 0 模块描述 / Module Description From subprocess module: """Subprocesses wit ...
- python实现并发服务器实现方式(多线程/多进程/select/epoll)
python实现并发服务器实现方式(多线程/多进程/select/epoll) 并发服务器开发 并发服务器开发,使得一个服务器可以近乎同一时刻为多个客户端提供服务.实现并发的方式有多种,下面以多进 ...
- Python 之并发编程之进程上(基本概念、并行并发、cpu调度、阻塞 )
一: 进程的概念:(Process) 进程就是正在运行的程序,它是操作系统中,资源分配的最小单位. 资源分配:分配的是cpu和内存等物理资源 进程号是进程的唯一标识 同一个程序执行两次之后是两个进程 ...
- 并发,并行,线程,进程,GIL锁
1.并发和并行 并发: 同时做某些事,但是强调同一时段做多件事 如:同一路口,发生了车辆要同时通过路面的时间. 并行: 互不干扰的在同一时刻做多件事 如:同一时刻,同时有多辆车在多条车道上跑,即同时发 ...
- python之并发编程(线程\进程\协程)
一.进程和线程 1.进程 假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作),而此时CPU只能静静地等待任务A读取完数据才能继续执行,这样就白白浪费了CPU资源.是 ...
- Python的并发并行[0] -> 基本概念
基本概念 / Basic Concept 快速跳转 进程 / Process 线程 / Thread 协程 / Coroutine 全局解释器锁 / Global Interpreter Lock ...
- python 之 并发编程(进程池与线程池、同步异步阻塞非阻塞、线程queue)
9.11 进程池与线程池 池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务 池子内什么时候装进程:并发的任务属于计算密集型 池子内什么时候装线程:并发的任务属于I ...
- Python 之并发编程之进程下(事件(Event())、队列(Queue)、生产者与消费者模型、JoinableQueue)
八:事件(Event()) # 阻塞事件: e = Event() 生成事件对象e e.wait() 动态给程序加阻塞,程序当中是否加阻塞完全取决于该对象中的is_set() [默认返回值 ...
- Python 之并发编程之进程中(守护进程(daemon)、锁(Lock)、Semaphore(信号量))
五:守护进程 正常情况下,主进程默认等待子进程调用结束之后再结束守护进程在主进程所有代码执行完毕之后,自动终止kill -9 进程号 杀死进程.守护进程的语法:进程对象.daemon = True设置 ...
随机推荐
- USACO Section2.3 Money Systems 解题报告 【icedream61】
money解题报告------------------------------------------------------------------------------------------- ...
- 【Binary Tree Right Side View 】cpp
题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...
- python判断mongodb--find(),find_one()返回是否为空
conn = MongoClient('127.0.0.1', 27017)db = conn.diffcollection = db['test1']result = collection.find ...
- Windows下如何解决git bash的默认home目录路径问题
转自:http://blog.csdn.net/lucien_zhou/article/details/62069246 为了解决这个问题,我在网上找了好久,尝试过按网上其他人所述,修改 git 安装 ...
- python-成员修饰符
python的面相对象中,拥有3个成员,字段.方法.属性 class Foo: def __init__(self,name): #公有字段name在类中与类外均能调用 self.name = nam ...
- 操作App.config的类(转载)
http://www.cnblogs.com/yaojiji/archive/2007/12/17/1003191.html 操作App.config的类 public class DoConfig ...
- Flex学习笔记
Flex —— Flexible Box 弹性布局 用来为盒子模型提供灵活性 /* 块级元素 */ .box{ display: flex; } /* 行内元素 */ .box{ display: i ...
- resharper激活
1.解压后点击64位系统的IntelliJIDEALicenseServer_windows_amd64.exe 32位点击IntelliJIDEALicenseServer_windows ...
- jQuery基础知识点(上)
jQuery是一个优秀的.轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, Opera 9.0+),而jQuery2.0及后续版本将不再支 ...
- 搭建springmvc项目404,没扫描到包
搭建简单项目完成之后,曾经出现过一个问题 跳转报了404,控制台忘了没留啊... 反正意思就是说我配置有问题,导致没有扫描到注释的类 <context:component-scan base-p ...