【脚本语言系列】关于Python进程线程管理系统模块,你需要知道的事
2017-06-12 14:40
1476 查看
如何使用Python管理进程线程
进程的运行环境
# -*- coding:utf-8 -*- import os path = os.environ.get("PATH") print path
C:\Python27\;C:\Python27\Scripts;C:\Python35\;C:\Python35\Scripts\;C:\Windows\system32;C:\Windows
# -*- coding:utf-8 -*- import os for key in os.environ.keys(): print key, "\t", os.environ[key]
PATH C:\Python27\;C:\Python27\Scripts;C:\Python35\;C:\Python35\Scripts\; C:\Windows\system32;C:\Windows SYSTEMROOT C:\Windows SESSIONNAME Console PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW WINDIR C:\Windows HOMEDRIVE C:
创建进程
system函数(调用cmd后,返还控制权)# -*- coding:utf-8 -*- import os # create the process with "system" print os.system("dir")
exec函数族(执行命令后,接管控制权)
# -*- coding:utf-8 -*- import os # create the process with "os.execl" notepad = "C:\\Windows\\notepad.exe" os.execl(notepad,"newfile1.txt") notepad = "C:\\Windows\\not_notepad.exe" os.execl(notepad,"newfile2.txt")
终止进程
return语句sys.exit函数
# -*- coding:utf-8 -*- import sys # exit the process with "sys.exit" try: filename = sys.argv[1] print filename except: print "Usage:", sys.argv[0],"filename" sys.exit(1) return 0
-f File "<ipython-input-6-72d5801fa3fc>", line 10 return 0 SyntaxError: 'return' outside function
os.abort函数
# -*- coding:utf-8 -*- import os,sys # exit the process with "sys.exit" try: filename = sys.argv[1] print filename except: print "Usage:", sys.argv[0],"filename" os.abort() return 0
-f File "<ipython-input-5-eefc966778fc>", line 10 return 0 SyntaxError: 'return' outside function
什么是进程线程
进程是正在执行的程序,是执行任务的基本单元;线程是进程的执行单元;多数程序,只需一个主线程。
相关文章推荐
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python数据库访问专用模块,你需要知道的事
- 【脚本语言系列】关于PythonWeb客户端-服务器模块http-urllib,你需要知道的事情
- 【脚本语言系列】关于PythonWeb服务自动化webbrowser模块,你需要知道的事
- 【脚本语言系列】关于Python数据库处理MySQL, 你需要知道的事
- 【脚本语言系列】关于Python邮件服务poplib,你需要知道的事
- 【脚本语言系列】关于Python音频处理WMPlayerOCX,你需要知道的事情
- 【脚本语言系列】关于Python开发环境搭建,你需要知道的事
- 【脚本语言系列】关于PythonGUI界面PythonWin,你需要知道的事
- 【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
- 【脚本语言系列】关于Python邮件服务smtplib,你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pylint, 你需要知道的事
- 【脚本语言系列】关于Python文本解析XML,你需要知道的事
- 【脚本语言系列】关于Python统计分析statsmodel,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python机器学习scikits-learn,你需要知道的事
- 【脚本语言系列】关于Python测试工具unittest, 你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事