推荐学习书目
› 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
yanyuechuixue
V2EX  ›  Python

请问,用 Pool 进程池,能否写成一个进程完成之后执行一段代码之后再开新的进程?

  •  
  •   yanyuechuixue · May 9, 2016 · 3103 views
    This topic created in 3797 days ago, the information mentioned may be changed or developed.

    是这样,我用的进程都是互相独立的,我现在是通过使用进程的返回值来得到进程运行的结果(而不是通信,因为返回值比较容易) 我是把返回的值 append 到一个列表里,最后把列表里的数按照一定规则加起来。 但这样的话,如果线程足够多,就会让这个列表很大。占用的内存超过物理内存,太亏了。

    所以我在想,能不能完成一个进程就把这个进程的返回值先运算完了再开新的进程,这样就不用先存着结果等全部进程都结束了再运算了。

    但我没找到相关方法,所以来这里请教。

    现在的部分代码是:

    result=[]
    for NaborDec in range(-4,5):
        for b in Qso_Pairs_List[ra+NaborRa][dec+NaborDec]:
            if a[0] > b[0]:
                result.append(pool.apply_async(GetPairs_a,(a,b)))
    
    pool.close()
    pool.join()
    
    CorrUp=[[ 0 for x in range(BinsPart)]for y in range(BinsPart)]
    CorrDown=[[ 0 for x in range(BinsPart)]for y in range(BinsPart)]
    
    for res in result:
        for i in range(BinsPart):
            for j in range(BinsPart):
                CorrUp[i][j]=CorrUp[i][j] + res.get()[0][i][j]
                CorrDown[i][j]=CorrDown[i][j] + res.get()[1][i][j]
    
    2 replies  •  2016-05-10 10:41:52 +08:00
    binux
        1
    binux  
       May 9, 2016
    Pool.imap
    yanyuechuixue
        2
    yanyuechuixue  
    OP
       May 10, 2016
    @binux
    谢谢,虽然没看明白,我再看看去。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2357 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:05 · PVG 16:05 · LAX 01:05 · JFK 04:05
    ♥ Do have faith in what you're doing.