futex_uu状态下的python subprocess.Popen块

futex_uu状态下的python subprocess.Popen块,python,multithreading,subprocess,Python,Multithreading,Subprocess,我试图实现一些基本的调度,一次最多生成n个进程。Popen,并等待它们执行。为此,我做了: CHECKING_INTERVAL = 10 class StandAloneClient(object): """ Instead of communicating with a backend cluster, fire locally a new thread. """ max_thread_nr = 4 thread_pool = [] pendi

我试图实现一些基本的调度,一次最多生成n个进程。Popen,并等待它们执行。为此,我做了:

CHECKING_INTERVAL = 10

class StandAloneClient(object):
    """
    Instead of communicating with a backend cluster, fire locally a new thread.
    """
    max_thread_nr = 4
    thread_pool = []
    pending_operations = Queue.Queue(0)

    class OperationExecutor(threading.Thread):

        def run(self):
            """
            Get the required data from the operation queue and launch the operation.
            """
            while True:
                launch_data = StandAloneClient.pending_operations.get()
                if launch_data != None:
                    operation_id = launch_data[0]
                    user_name_label = launch_data[1]
                    LOGGER.debug("Launching operation " + str(operation_id) + " with name " + str(user_name_label))
                    ## Create a new process for the new launched operation
                    oper = ['python', '-m', 'processRunner', str(operation_id), user_name_label]
                    launched_process = Popen(oper, stdout=PIPE, stdin=PIPE, stderr=PIPE)
#                    launched_process.wait()
#                    while launched_process.poll() is None:
#                        sleep(CHECKING_INTERVAL)
#                        LOGGER.debug("Operation id=%s is still running. Going to sleep for %s seconds."%(operation_id,
#                                                                                                         CHECKING_INTERVAL))
                    LOGGER.debug("===========================================================")
                    LOGGER.debug("Finished operation %s succesfully."%(operation_id,))


    def __init__(self):
        """
        If there are still empty spots create a new executor thread.
        """
        for _ in xrange(self.max_thread_nr - len(self.thread_pool)):
            new_executor = StandAloneClient.OperationExecutor()
            self.thread_pool.append(new_executor)
            new_executor.start()

    @staticmethod
    def execute(operation_id, user_name_label="Unknown"):
        """Start asynchronous operation locally"""
        StandAloneClient.pending_operations.put((operation_id, user_name_label))
我正在通过以下方式向队列添加操作:

StandAloneClient().execute(...)
现在我已经评论了我认为可能由于某种原因而阻塞线程的部分。但即便如此,似乎没有一个孩子是永远不会出生的。执行一直持续到结束,我已经检查了日志,
processRunner.py
完成了它需要执行的所有操作,直到结束,但是如果我执行
ps-el | grep python
我仍然可以看到生成的所有进程:

    0 S  1000   755     1  5  80   0 - 548314 poll_s pts/0   00:00:13 python
0 S  1000  1198   755  4  80   0 - 280172 futex_ pts/0   00:00:09 python
0 S  1000  1201   755  4  80   0 - 280176 futex_ pts/0   00:00:09 python
0 S  1000  1206   755  4  80   0 - 280230 futex_ pts/0   00:00:09 python
0 S  1000  1215   755  4  80   0 - 280198 futex_ pts/0   00:00:09 python
0 S  1000  1216   755  4  80   0 - 281669 futex_ pts/0   00:00:09 python
0 S  1000  1221   755  4  80   0 - 280201 futex_ pts/0   00:00:09 python
0 S  1000  1231   755  4  80   0 - 281668 futex_ pts/0   00:00:09 python
0 S  1000  1240   755  4  80   0 - 280229 futex_ pts/0   00:00:09 python
0 S  1000  1257   755  4  80   0 - 280201 futex_ pts/0   00:00:09 python
我正在Fedora16机器上用Python2.7.2进行测试。任何建议>

问候,,
Bogdan

您的线程不是守护进程。它们在主线程退出后继续生存。它们永远被阻止在
挂起的\u操作中。

为什么要使用子进程而不是多进程来在多个进程中运行Python代码?旁注:对于这样一个简单的任务,您的代码是复杂的