python 多线程

场景: 在项目调试过程中总出现 python 进程被挂起的(S)状态. 目前暂以为是由于 开的进程池并没有关闭(pool.close())和阻塞当前进程(pool.join()),等待进一步的测试结果。

ps -ef| grep python

ps -aux|grep python

http://blog.csdn.net/seetheworld518/article/details/49639651

http://www.cnblogs.com/congbo/archive/2012/08/23/2652433.html

-------动手实例-------

进程池的运行必须在 程序入口或其他方法中,不可以直接在外面运行

import multiprocessing
import time


def a(num):
    print 'begin  %s' % num, time.time()
    time.sleep(2)
    print 'end  %s' % num, time.time()
    return num * num


if __name__ == '__main__':

    pool = multiprocessing.Pool(processes=2)
    parameters = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

    results = pool.map(a, parameters)

    pool.close() # if Notes this two line, the result will differents.
    pool.join() #

    print 'results',results
    # for result in results:
    #     print result

 

posted @ 2017-09-07 15:52  百变小超  阅读(138)  评论(0编辑  收藏  举报