您的位置:首页 > 编程语言 > Python开发

【脚本语言系列】关于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


什么是进程线程

进程是正在执行的程序,是执行任务的基本单元;

线程是进程的执行单元;多数程序,只需一个主线程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐