我正在尝试实现一些基本的调度来产生最大n过程。波本然后等待它们执行。为此我做了:
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))
我通过以下方式向队列添加操作:
^{pr2}$现在我已经评论了我认为可能因为某种原因而阻塞线程的部分。但即便如此,似乎没有一个孩子的出生永远结束。执行直到结束,我检查了日志,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来尝试这个。有什么建议>
谨致问候, 博格丹
你的线程不是守护进程。它们在主线程退出后继续生存。它们永远被阻止在
pending_operations.get()
。在相关问题 更多 >
编程相关推荐