Python实战之子进程
来源:互联网 发布:app软件大全 编辑:程序博客网 时间:2024/04/28 20:05
Python实战之子进程
最近做一个小工具需要用到Python的字进程,看了一下Python的API,subprocess模块可以就可以实现,并且还挺简单
要实现一个简单的自进程,只需要
- import sys, os, subprocess
- p = subprocess.Popen(["ls", "-l"], shell=False, stdin=None, stdout=None, stderr=None)
- print p.pid
- print p.returncode
此时,pid将是子进程的ID号,如果子进程正常推出,returncode将会是None。
也可以使用以下代码创建子进程
- import sys, os, subprocess
- retcode=subprocess.call(["ls", "-l"], shell=False, stdin=None, stdout=None, stderr=None)
- print retcode
此时,如果子进程正常推出,retcode将会是0。
另外,如果期望不要输出创建出来的子进程的打印信息,可以将方法参数中的stdout和stderr设置为subprocess.PIPE。
以下是一个小例子,创建一个子进程p1,然后再创建一个子进程p2用来结束p1的执行。
- import sys, os, subprocess
- p1 = subprocess.Popen(["sleep", "60"], shell=False, stdin=None, stdout=None, stderr=None)
- print p1.pid
- print p1.returncode
- p2 = subprocess.Popen(["kill", str(p1.pid)], shell=False, stdin=None, stdout=None, stderr=None)
- print p2.pid
- print p2.returncode
0 0
- Python实战之子进程
- Python实战之子进程
- Python学习笔记13:标准库之子进程(subprocess包)
- (初级)并发服务器之子进程
- TCL学习之子进程调用--open&exec
- Linux---之子父进程,及exec命令
- OS之子进程简单模拟shell解释环境
- OS之子进程简单模拟shell解释环境(再次)
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- [Python 实战]
- Perl多进程实战
- 【iOS初学笔记】TextField设置内填充(padding or inset)
- Python实战之多线程编程threading.Thread
- codeforces 283C Coin Troubles (神级思维+判环+背包)
- HDU 3665 Seaside (Floyd 最短路)
- Python实战之多线程编程thread模块
- Python实战之子进程
- MySQL应用
- C++之面向程序设计浅析
- 我不是会学会玩
- HDU356 Eight II(康拓展开+预处理)
- PowerShell常用的轉義符、比較符、邏輯操作符
- Python多线程编程
- C++之多态
- mysql存储过程详解