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设置 ...
随机推荐
- Unicode字符图标
http://unicode-table.com/cn/#control-character
- 剑指Offer - 九度1506 - 求1+2+3+...+n
剑指Offer - 九度1506 - 求1+2+3+...+n2013-11-29 19:22 题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switc ...
- Visual Studio 提示某个dll文件(已在Microsoft Visual Studio 外对该文件进行了修改,是否重新加载它)
如题: Visual Studio 提示某个dll文件(已在Microsoft Visual Studio 外对该文件进行了修改,是否重新加载它) 如果选择“是”,那恭喜你,第二次生成的时候,引用这个 ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- Mysql DISTINCT问题
问题描述 因为要设计一个数据库表,进行一个倒序去重的操作. 例如: id Name 1 B 2 A 3 A 4 C 5 C 6 B 场景:例如说我们需要得到一个用户的搜索记录,那么肯定不会仅仅根据时间 ...
- fragment中的WebView返回上一页
public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...
- jquery select chosen禁用某一项option
$("#tbParBudCode").chosen().change(function () { $("#tbParBudCode option[value='" ...
- 【bzoj2879】[Noi2012]美食节 费用流+动态加边
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他 ...
- margin-top影响父元素定位
写样式时无意中发现margin-top会影响到父元素的定位,下面是示例: HTML代码: <div class="demo"> <div class=" ...
- 如何写出高质量的JavaScript代码
优秀的Stoyan Stefanov在他的新书中(<Javascript Patterns>)介绍了很多编写高质量代码的技巧,比如避免使用全局变量,使用单一的var关键字,循环式预存长度等 ...