V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
salmd
V2EX  ›  Python

传递 redis 对象给子进程,遇到的问题,求指点

  •  
  •   salmd · 2016-09-28 19:38:55 +08:00 · 1825 次点击
    这是一个创建于 2766 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 查了很多资料,没有找到准确的答案,求各位大神解惑
    • 有一个需求,是把 redis 的连接传到子进程里面,就产生了很奇怪的情况,没有报错,子进程也没有启动。。。
    • 把具体出问题地方整理如下
    import redis
    from multiprocessing import Pool
    import random
    import time
    
    def redisInput(redisOb):
        time.sleep(2)
        print 'test process start'
        print redisOb
    
    
    if __name__ == '__main__':
        print 'Input...'
        redisOb = redis.Redis(host='10.237.36.108', port=3333, db=0)
        p = Pool(5)
        for x in range (5):
            args = (redisOb,)
            #args = (1,)
            print type(args)
            p.apply_async(redisInput,args)
    
        p.close()
        p.join()
        print 'The end'
    

    问题出在 p.apply_async 传递的参数上,如果把 args = (redisOb,),改成 args = (1,);子进程就正常了

    求各位达人能帮忙看一下。

    5 条回复    2016-09-28 22:22:29 +08:00
    fds
        1
    fds  
       2016-09-28 19:48:35 +08:00
    ……不能传这个吧。你可以把 redis 的参数传进去,每个进程自己创建连接。
    kfll
        2
    kfll  
       2016-09-28 20:17:13 +08:00   ❤️ 2
    salmd
        3
    salmd  
    OP
       2016-09-28 21:23:47 +08:00
    @fds 已经这么实现了,只是好奇这个问题
    salmd
        4
    salmd  
    OP
       2016-09-28 21:46:54 +08:00
    @kfll 还请帮忙指出大概位置或者关键字,渣英语实在没找到
    BiggerLonger
        5
    BiggerLonger  
       2016-09-28 22:22:29 +08:00 via Android
    pickle
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3318 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 12:49 · PVG 20:49 · LAX 05:49 · JFK 08:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.