python skill
- 动态load module
- 子进程
- stdout, stderr重定向
sample code
def sub_proc(conf, task):
import importlib
_task = {}
_task.update(task)
task['status'] = 'running'
pid = os.getpid()
stdout = open('stdout', 'w')
stderr = open('stderr', 'w')
os.dup2(stdout.fileno(), sys.stdout.fileno())
os.dup2(stderr.fileno(), sys.stderr.fileno())
print 'worker:%s conf:%s task:%s' % (pid, json.dumps(conf, indent=4), json.dumps(_task, indent=4))
__input = {'a':1, 'b':2}
hd = importlib.import_module('test_py_lib.util')
__cls = getattr(hd, 'Plugin')
_c = __cls()
_c.process(**__input)
getattr(_c, "run")(**__input)
ff = getattr(_c, "ff") if hasattr(_c, 'ff') else None
print ff
task['status'] = 'done'
stdout.close()
stderr.close()
def main():
manager = Manager()
task = manager.dict()
conf = {'conf1':'aaaaa'}
task['status']= 'ready'
p = Process(target= sub_proc, args = (conf, task, ))
p.start()
for i in xrange(0, 10):
print 'main> %s' % i
if p.is_alive():
time.sleep(1)
else:
break
else:
print 'main> worker timeout, kill it'
p.terminate()
p.join()
print 'main> worker done, return code:%s' % p.exitcode
_task = {}
_task.update(task)
print 'main> worker status:%s' % json.dumps(_task, indent=4)
- test_py_lib/util/_init_.py
class Plugin(object):
def __init__(self):
print 'Plugin.__init__'
pass
def run(self, **kwargs):
print 'Plugin.run'
for k,v in kwargs.items():
print 'k:%s v:%s' % (k, v)
def process(self, **kwargs):
print 'Plugin.process'
for k,v in kwargs.items():
print 'k:%s v:%s' % (k, v)
- main.py
+---- test_py_lib/
+---- __init__.py
+---- util/
+---- __init__.py