简体   繁体   English

并行Python-如何使用输出数据?

[英]Parallel python - how to use output data?

Hey im new to Parallel python, and was writing a simple code that adds numbers together in parallel, the code works fine to add the numbers, but im wondering how you go about using the output from the jobs after its done, if possible? 嗨,我是Parallel python的新手,当时正在编写一个简单的代码,以并行方式将数字加在一起,该代码可以很好地添加数字,但是我想知道如果可能的话,如何使用作业的输出? my code is as follows: 我的代码如下:

import sys, time
import pp
import numpy
x = numpy.arange(-20.0,20.0,0.5)
grav = []
gravity = numpy.zeros([4,1])
def function(raw_input,x,grav):
    f = 0
    for i in numpy.arange(len(x)):
        f+=1
    a=raw_input[0]
    b=raw_input[1]
    c=raw_input[2]
    d=raw_input[3]
    grav.append((a+b+c+d)+f)
    return grav
for i in numpy.arange(2):
    # tuple of all parallel python servers to connect with
    ppservers = ()
    #ppservers = ("10.0.0.1",)

    if len(sys.argv) > 1:
        ncpus = int(sys.argv[1])
        # Creates jobserver with ncpus workers
        job_server = pp.Server(ncpus, ppservers=ppservers)
    else:
        # Creates jobserver with automatically detected number of workers
        job_server = pp.Server(ppservers=ppservers)

    print "Starting pp with", job_server.get_ncpus(), "workers"
    start_time = time.time()

    # The following submits 4 jobs and then retrieves the results
    puts = ([1,2,3,4], [3,2,3,4],[4,2,3,6],[2,3,4,5])

    jobs = [(raw_input, job_server.submit(function,(raw_input,x,grav), (), ("numpy",))) for raw_input in puts]
    for raw_input, job in jobs:
        print "Sum of numbers", raw_input, "is", job()
    print grav
    print "Time elapsed: ", time.time() - start_time, "s"
    job_server.print_stats()
    print gravity
    #gravity[i] = grav

this prints out the 4 results, which are 90,92,95,94, and gives the stats etc. so my question is how can i then use the 4 result numbers, i want them to be dumped into the array called gravity that's there but i cant figure out how. 这将打印出4个结果,分别是90,92,95,94,并提供统计信息等。所以我的问题是我该如何使用4个结果数,我希望将它们转储到存在重力的数组中但我不知道怎么做。 Thanks 谢谢

You need to store the result of the job() function. 您需要存储job()函数的结果。 Try the following code: 尝试以下代码:

import sys, time
import pp
import numpy
x = numpy.arange(-20.0,20.0,0.5)
grav = []
gravity = numpy.zeros([4,1])
def function(raw_input,x,grav):
    f = 0
    for i in numpy.arange(len(x)):
        f+=1
    a=raw_input[0]
    b=raw_input[1]
    c=raw_input[2]
    d=raw_input[3]
    grav.append((a+b+c+d)+f)
    return grav

jobsList = []

for i in numpy.arange(2):
    # tuple of all parallel python servers to connect with
    ppservers = ()
    #ppservers = ("10.0.0.1",)

    if len(sys.argv) > 1:
        ncpus = int(sys.argv[1])
        # Creates jobserver with ncpus workers
        job_server = pp.Server(ncpus, ppservers=ppservers)
    else:
        # Creates jobserver with automatically detected number of workers
        job_server = pp.Server(ppservers=ppservers)

    print "Starting pp with", job_server.get_ncpus(), "workers"
    start_time = time.time()

    # The following submits 4 jobs and then retrieves the results
    puts = ([1,2,3,4], [3,2,3,4],[4,2,3,6],[2,3,4,5])

    jobs = [(raw_input, job_server.submit(function,(raw_input,x,grav), (), ("numpy",))) for raw_input in puts]
    for raw_input, job in jobs:
        r = job()
        jobsList.append(r)
        print "Sum of numbers", raw_input, "is", r
    print grav
    print "Time elapsed: ", time.time() - start_time, "s"
    job_server.print_stats()
    print gravity
    for job in jobsList:
        print job

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM