English 中文(简体)
甲型六氯环己烷 在叫WaitForMultipleObjects之后,如何获得程序返回地位?
原标题:Python/Windows/ctypes: How to get process return status after calling WaitForMultipleObjects?

我的用法与

我在此试图修改他的方案。 我是视窗地和陈列型的游客,因此,我希望我没有做任何事情。

import ctypes, subprocess
from random import randint
import os
SYNCHRONIZE=0x00100000
PROCESS_QUERY_INFORMATION=0x0400
INFINITE = -1
numprocs = 5
handles = {}

class Err(BaseException): pass

for i in xrange(numprocs):
    sleeptime = randint(5,10)
    p = subprocess.Popen([r"c:MinGWmsys1.0insleep.exe", str(sleeptime)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
    h = ctypes.windll.kernel32.OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, False, p.pid)
    handles[h] = p.pid
    print "Spawned Process %d" % p.pid

while len(handles) > 0:
    print "Waiting for %d children..." % len(handles)
    arrtype = ctypes.c_long * len(handles)
    handle_array = arrtype(*handles.keys())
    ret = ctypes.windll.kernel32.WaitForMultipleObjects(len(handle_array), handle_array, False, INFINITE)
    h = handle_array[ret]
    print "Process %d done" % handles[h]
    i = ctypes.c_int(0)
    pi = ctypes.pointer(i)
    if ctypes.windll.kernel32.GetExitCodeProcess(h, pi) == 0:
    err = ctypes.windll.kernel32.GetLastError()
        raise Err("GetExitCodeProcess: %d" % (err))
    print "Status code is: %d" % (i)
    if ctypes.windll.kernel32.CloseHandle(h) == 0:
    err = ctypes.windll.kernel32.GetLastError()
    raise Err("CloseHandle: %d" % (err))
    del handles[h]
print "All done!

但是,在我执政的时候,我失败了:

Traceback (most recent call last):
  File "test.py", line 30, in <module>
    raise Err("GetExitCodeProcess: %d" % (err))
__main__.Err: GetExitCodeProcess: 6

看来,error 6 is ERR_INVALID_HANDLE。 但是,我不敢肯定的是,如果我发表以下意见,则要求获得<条码>PROCESS_questRY_INFORMATION的许可和<条码>CloseHandle(>的打字。 GetExitCodeProcess()。

任何想法? 在等待一些进程之后,我如何获得地位法?

问题回答

采用这种硬墙法(与你一样,只有一些 in子固定下来,可以睡觉。 移除:

import ctypes, subprocess
from random import randint
import os
SYNCHRONIZE=0x00100000
PROCESS_QUERY_INFORMATION=0x0400
INFINITE = -1
numprocs = 5
handles = {}

class Err(BaseException): pass

for i in xrange(numprocs):
    sleeptime = randint(5,10)
    p = subprocess.Popen([r"sleep.exe", str(sleeptime)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
    h = ctypes.windll.kernel32.OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, False, p.pid)
    handles[h] = p.pid
    print "Spawned Process %d" % p.pid

while len(handles) > 0:
    print "Waiting for %d children..." % len(handles)
    arrtype = ctypes.c_long * len(handles)
    handle_array = arrtype(*handles.keys())
    ret = ctypes.windll.kernel32.WaitForMultipleObjects(len(handle_array), handle_array, False, INFINITE)
    h = handle_array[ret]
    print "Process %d done" % handles[h]
    i = ctypes.c_int(0)
    pi = ctypes.pointer(i)
    if ctypes.windll.kernel32.GetExitCodeProcess(h, pi) == 0:
        err = ctypes.windll.kernel32.GetLastError()
        raise Err("GetExitCodeProcess: %d" % (err))
    print "Status code is: %d" % i.value
    if ctypes.windll.kernel32.CloseHandle(h) == 0:
        err = ctypes.windll.kernel32.GetLastError()
        raise Err("CloseHandle: %d" % (err))
    del handles[h]

print "All done!"

给我这一产出:

C:	mppyt>python -V
Python 2.7.3

C:	mppyt>python a.py
Spawned Process 4368
Spawned Process 268
Spawned Process 5792
Spawned Process 4744
Spawned Process 4484
Waiting for 5 children...
Process 5792 done
Status code is: 0
Waiting for 4 children...
Process 4484 done
Status code is: 0
Waiting for 3 children...
Process 268 done
Status code is: 0
Waiting for 2 children...
Process 4368 done
Status code is: 0
Waiting for 1 children...
Process 4744 done
Status code is: 0
All done!

因此,它似乎正在发挥作用。 问题究竟是何在? 你们使用哪位顾问(是Vista)? 你们是否拥有最新版本(2.7.3)? 你们的“ep”是否打破了某种?? 你们是否试图另谋?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签