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
billgreen1
V2EX  ›  Python

python multiprocessing pool 遇到问题

  •  
  •   billgreen1 · 2016-01-19 15:32:40 +08:00 · 4383 次点击
    这是一个创建于 3020 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为经常要用到并行程序,我写了一个 decorator

    from functools import wraps
    from pathos.multiprocessing import ProcessingPool as Pool # to tackle pickle problem, using pathos.multiprocessing rather than multiprocessing
    
    def parallel(func):
        @wraps(func)
        def apply(values):
            pool = Pool()
            result =  pool.map(func, values)
            pool.close()
            pool.join()
            return result
        return apply
    

    程序中我这样使用

    @parallel
    def square(x):
        return x ** 2
    
    @parallel
    def cubic(x):
        return x ** 3
    
    if __name__ == '__main__':
        x = range(10)
        ret1 = square(x)
        ret2 = cubic(ret1)
        print(ret2)
    

    会出现

    result =  pool.map(func, values)
      File "/home/phil/anaconda2/lib/python2.7/site-packages/pathos/multiprocessing.py", line 133, in map
        return _pool.map(star(f), zip(*args)) # chunksize
      File "/home/phil/anaconda2/lib/python2.7/site-packages/multiprocess/pool.py", line 250, in map
        assert self._state == RUN
    AssertionError
    

    问题是, 如果注释掉 square 或者 cubic 任意一个的装饰器, 程序是可以运行的。

    请教该如何做?

    1 条回复    2016-01-19 15:52:15 +08:00
    billgreen1
        1
    billgreen1  
    OP
       2016-01-19 15:52:15 +08:00
    注释掉
    pool.close()
    pool.join()
    可行。

    但是大多数程序给出的代码都有这两句的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2814 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:16 · PVG 14:16 · LAX 23:16 · JFK 02:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.