• PythonPP+lambda:示例



        直接上代码。 python PP 下载, 可以到官网下载 Python PP 下载 

        lambda 能做的, 基本上普通函数都可以做到。 lambda 的主要作用是简化表达式, 并且似乎尤其适合于表达科学计算公式。 结合 map, reduce 等函数,可以获得更强大而“优雅”的表达能力。 这里的“优雅”并不一定表示易理解性,而常常是因为一句短小精炼的代码使得人眼前一亮。   

        以下代码演示 lambda 的用法及 python 并发编程。    

     

    import pp
    from multiprocessing import Process, Pool, Pipe
    
    def addNFunc(n):
        return lambda x:x+n
    
    def fac(n):
        if n <= 1:
            return 1
        return reduce(lambda x,y: x*y, map(addNFunc(1), range(n)))
    
    def computingFacOfRange(begin, end):
        for i in map(addNFunc(begin), range(end-begin+1)):
            print i, '! =', fac(i)
    
    def another():
        print 'another quick job'
     
    def  QuadraticSum(n, m):
        ''' compute 1^m + 2^m + ... + n^m '''
        if n <= 1:
            return 1
        return reduce(lambda x,y: x+y**m, map(addNFunc(1), range(n)))
    
    def expressQS(n,m):
        if n <= 1:
            return '1'
        return reduce(lambda x,y: "%s+%s" %(str(x),str(y)+'^'+str(m)), map(addNFunc(1), range(n)))
    
    def obtainQuadraticSum(n, m):
        return "%s=%s" % (expressQS(n, m), QuadraticSum(n,m))
    
    def obtainQuadraticSumByPipe(conn, n, m):
        conn.send(obtainQuadraticSum(n, m))
    
    def printQuadraticSum(n, m):
        print obtainQuadraticSum(n, m)
    
    def testQuadraticSum():
        for n in [1,2,3,4,5]:
            for m in [-1,1,2,3]:
                printQuadraticSum(n, m)
                      
    def usingPP():
        ''' using pp module for python concurrent programming '''
        nworkers = 10
        ppservers = ()
        job_server = pp.Server(nworkers, ppservers=ppservers)
    
        print "Starting pp with", job_server.get_ncpus(), "workers"
    
        jobs = []
        for i in range(nworkers):
            jobs.append(job_server.submit(computingFacOfRange, (i*10+1, (i+1)*10), (fac,addNFunc,)))
    
        jobs.append(job_server.submit(another, ()))
    
        for job in jobs:
            job()
        
        job_server.print_stats()
        
    
    def usingMultiprocess():
        ''' using multiprocessing module for python concurrent programming '''
        num = 100
        processes = []
    
        print '************ using original process ***********'
        input_conn, output_conn = Pipe()
        for m in [-1,1,2,3]:
            p = Process(target=obtainQuadraticSumByPipe, args=(input_conn, num,m,))
            p.start()
            print output_conn.recv()
    
        print '------------- using Pool -------------'
        pool = Pool(processes=4)
        for m in [-1,1,2,3]:
            pool.apply(printQuadraticSum, (num,m))
           
    
    if __name__ == '__main__':
        testQuadraticSum()
        usingPP()
        usingMultiprocess()
  • 相关阅读:
    General procedures for upgrading Debian
    Install documentation for GCC on Debian Buster
    本地学习环境minikube安装
    解决phpstorm中ftp读取远程目录出现嵌套循环情况
    Linux 文件句柄&文件描述符
    VBA如何实现筛选条件之“排除某些值”
    利用Python多线程快速爬取某网站数据
    利用云服务器搭建远程办公访问(frp实现内网穿透)
    printf()函数
    malloc和calloc的区别
  • 原文地址:https://www.cnblogs.com/lovesqcc/p/4037665.html
Copyright © 2020-2023  润新知