python 动态加载,子进程,重定向

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))

	# load a plugin 
    __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) # an exception if _c has no 'run'  
    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) # get info from sub process
    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)
  • file layout
- main.py 
+---- test_py_lib/ 
        +---- __init__.py 
        +---- util/ 
                +---- __init__.py
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值