📄 stages.py.svn-base
字号:
# Copyright (C) 2001 Vladimir Prus. Permission to copy, use, modify, sell and# distribute this software is granted, provided this copyright notice appears# in all copies and modified versions are clearly marked as such. This software# is provided "as is" without express or implied warranty, and with no claim as# to is suitability for any purpose.# Basic approach. The are several copies of this script running on different# machines. They all should have access to the same directory, using# network file system. As result of run, several "stage" directories are# created under to top-level one. For example, they could be "src",# "src-complete", "bin-linux", "bin-windows". To generate each stage,# master copy of this script invokes a method (over XML-RPC), in all# other scripts, which do whatever action is appropriate for the system# they run on. For example, script running in Linux might add# "bootstrap-linux.sh" file somewhere, while script running on Win would# add "bootstrap-win.bat". Execution of the same stage in different scripts# is done in parallel, but they are synchronized before next stage is# created.#from threading import Thread, Condition, Semaphorefrom SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandlerfrom xmlrpclib import ServerProxy, Error, Binaryimport stringdef run_stages(stages, remote_scripts): """Runs stages locally and on the specified remote scripts. 'stages' -- a list of names of function which implement stages. 'remote_scripts' -- a list of remote addresses where this script runs. If None, locally """ targets = [] for rs in remote_scripts: if rs is None: targets.append(None) elif string.find(rs, ":") != -1: targets.append(ServerProxy("http://" + rs)) else: targets.append(ServerProxy("http://" + rs + ":7800")) class Run_stages(Thread): """Runs stages in several threads, synchronizing stage completition.""" def __init__(self, stages, target, allow_run_sem, state_condition, num_threads): Thread.__init__(self) self.stages = stages self.target = target self.allow_run_sem = allow_run_sem self.state_condition = state_condition self.num_threads = num_threads def run(self): # Hope that read only access is thread-safe for s in self.stages: # Wait till we're allowed to run print "Waiting for a chance to run" self.allow_run_sem.acquire() print "Running again" if self.target is None: s() else: print self.target print s.__name__ getattr(self.target, s.__name__)() self.state_condition.acquire() self.state_condition.value = self.state_condition.value + 1 self.state_condition.notify() self.state_condition.release() allow_run_sem = Semaphore(0) state_condition = Condition() state_condition.value = 0 threads = [] num_threads = len(remote_scripts)+1 for t in targets + [None]: threads.append(Run_stages(stages, t, allow_run_sem, state_condition, num_threads)) for t in threads: t.setDaemon(1) t.start() for s in stages: print "Stage", s for i in range(num_threads): allow_run_sem.release() state_condition.acquire() while state_condition.value != num_threads: state_condition.wait() state_condition.value = 0 state_condition.release()def start_server(stages): """Starts a server handling the specified stages.""" server = SimpleXMLRPCServer(('', 7800)) for s in stages: server.register_function(s) server.serve_forever()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -