windows平台下如何用Python杀进程

#from : http://www.joyloft.net/?p=1031
# "这段代码来自于一个很简单的场景:
#  在python中调一个程序。给这个程序设置一个timeout
#  假如一段时间程序还没有返回,就杀掉这个这个新开的进程。
#  本来以为我肯定不是第一个遇到这个问题的人,
#  但是网上搜了好久都没找到完整的办法,自己搞了一个。"

import ctypes
import sys

TH32CS_SNAPPROCESS = 0 x00000002
class PROCESSENTRY32( ctypes . Structure ):
     _fields_ = [( "dwSize" , ctypes . c_ulong ),
                 ( "cntUsage" , ctypes . c_ulong ),
                 ( "th32ProcessID" , ctypes . c_ulong ),
                 ( "th32DefaultHeapID" , ctypes . c_ulong ),
                 ( "th32ModuleID" , ctypes . c_ulong ),
                 ( "cntThreads" , ctypes . c_ulong ),
                 ( "th32ParentProcessID" , ctypes . c_ulong ),
                 ( "pcPriClassBase" , ctypes . c_ulong ),
                 ( "dwFlags" , ctypes . c_ulong ),
                 ( "szExeFile" , ctypes . c_char * 260 )]

def getProcList ():
    CreateToolhelp32Snapshot = ctypes . windll . kernel32 . CreateToolhelp32Snapshot
    Process32First = ctypes . windll . kernel32 . Process32First
    Process32Next = ctypes . windll . kernel32 . Process32Next
    CloseHandle = ctypes . windll . kernel32 . CloseHandle
    hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS , 0)
    pe32 = PROCESSENTRY32()
    pe32 . dwSize = ctypes . sizeof( PROCESSENTRY32)
    if Process32First( hProcessSnap , ctypes . byref( pe32)) == False :
        print >> sys . stderr , "Failed getting first process."
        return
    while True :
        yield pe32
        if Process32Next( hProcessSnap , ctypes . byref( pe32)) == False :
            break
    CloseHandle( hProcessSnap)

def getChildPid( pid ):
    procList = getProcList()
    for proc in procList :
        if proc . th32ParentProcessID == pid :
            yield proc . th32ProcessID
   
def killPid( pid ):
    childList = getChildPid( pid)
    for childPid in childList :
        killPid( childPid)
    handle = ctypes . windll . kernel32 . OpenProcess( 1 , False , pid)
    ctypes . windll . kernel32 . TerminateProcess( handle , 0)


if __name__ == '__main__' :
    args = sys . argv
    if len( args) > 1 :
        pid = int( args [ 1 ])
        killPid( pid)
    else :
        procList = getProcList()
        for proc in procList :
            print proc . szExeFile + '  ' + str( proc . th32ParentProcessID) + '  ' + str( proc . th32ProcessID)
   

#----------------------
#
# Usage demo
#
#----------------------
import subprocess
import time

#import winproc

timeout = 2
process = subprocess . Popen( "cmd /k ping localhost -t" , shell = True)
start = int( time . time())
while process . poll() == None :
    now = int( time . time())
    if int ( now - start) > timeout :
        pid = process . pid
        break

winproc . killPid( pid)
       
print "End"

 

原帖:http://fayaa.com/code/view/538/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值