Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-函数无法在新线程中运行_Python_Multithreading_Wmi - Fatal编程技术网

Python-函数无法在新线程中运行

Python-函数无法在新线程中运行,python,multithreading,wmi,Python,Multithreading,Wmi,我正试图使用此函数在windows上终止notepad.exe进程: import thread, wmi, os print 'CMD: Kill command called' def kill(): c = wmi.WMI () Commands=['notepad.exe'] if Commands[0]!='All': print 'CMD: Killing: ',Commands[0] for process in c.Wi

我正试图使用此函数在windows上终止
notepad.exe
进程:

import  thread, wmi, os
print 'CMD: Kill command called'
def kill():
    c = wmi.WMI ()
    Commands=['notepad.exe']

    if Commands[0]!='All':
        print 'CMD: Killing: ',Commands[0]
        for process in c.Win32_Process ():
          if process.Name==Commands[0]:
              process.Terminate()
    else:
        print 'CMD: trying to kill all processes'
        for process in c.Win32_Process ():
            if process.executablepath!=inspect.getfile(inspect.currentframe()):           
                try:
                    process.Terminate()
                except:
                    print 'CMD: Unable to kill: ',proc.name

kill() #Works               
thread.start_new_thread( kill, () ) #Not working
当我像这样调用函数时,它就像一个符咒:

kill()

但是当在新线程中运行函数时,它崩溃了,我不知道为什么

import  thread, wmi, os
import pythoncom
print 'CMD: Kill command called'
def kill():
    pythoncom.CoInitialize()
    . . .

在线程中运行Windows函数可能很棘手,因为它通常涉及COM对象。使用
pythoncom.CoInitialize()
通常允许您这样做。另外,你可能想看看图书馆。它比线程更容易处理。

有几个问题(编辑:第二个问题在我开始回答“MikeHunter”之后已经解决了,所以我将跳过这个问题):

首先,您的程序在启动线程后立即结束,并带着线程。我认为这不是一个长期问题,因为这可能是更大问题的一部分。为了避免这种情况,您可以通过在脚本末尾添加一个
time.sleep()
调用(例如,以5秒作为睡眠时间)来模拟其他一些东西来保持程序运行

这将允许程序给我们一个有用的错误,在您的情况下是:

CMD: Kill command called
Unhandled exception in thread started by <function kill at 0x0223CF30>
Traceback (most recent call last):
  File "killnotepad.py", line 4, in kill
    c = wmi.WMI ()
  File "C:\Python27\lib\site-packages\wmi.py", line 1293, in connect
    raise x_wmi_uninitialised_thread ("WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex]")
wmi.x_wmi_uninitialised_thread: <x_wmi: WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex] (no underlying exception)>
CMD:Kill命令调用
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
kill中第4行的文件“killnotepad.py”
c=wmi.wmi()
文件“C:\Python27\lib\site packages\wmi.py”,第1293行,在connect中
raise x_wmi_uninitialized_thread(“wmi返回语法错误:您可能在线程内运行,而没有首先调用pythoncom.CoInitialize[Ex]”)
wmi.x_wmi_未初始化_线程:当你说“它崩溃”时,你的确切意思是什么?