并行python:通信管道读取错误

并行python:通信管道读取错误,python,Python,我正在我的一个程序中使用并行Python。 当我通过CLI运行Python程序时,它运行得很好。 但当我通过调试器运行它时,它会抛出下一个错误 File "D:/Unief/Thesis/deepmedic-master\deepmedic\trainValidateTestVisualiseParallel.py", line 1063, in do_training job_server = pp.Server(ncpus=1, ppservers=ppservers) # Cr

我正在我的一个程序中使用并行Python。 当我通过CLI运行Python程序时,它运行得很好。 但当我通过调试器运行它时,它会抛出下一个错误

  File "D:/Unief/Thesis/deepmedic-master\deepmedic\trainValidateTestVisualiseParallel.py", line 1063, in do_training
    job_server = pp.Server(ncpus=1, ppservers=ppservers) # Creates jobserver with automatically detected number of workers
  File "D:\Anaconda2\lib\site-packages\pp-1.6.4-py2.7.egg\pp.py", line 339, in __init__
    self.set_ncpus(ncpus)
  File "D:\Anaconda2\lib\site-packages\pp-1.6.4-py2.7.egg\pp.py", line 503, in set_ncpus
    range(ncpus - len(self.__workers))])
  File "D:\Anaconda2\lib\site-packages\pp-1.6.4-py2.7.egg\pp.py", line 138, in __init__
    self.start()
  File "D:\Anaconda2\lib\site-packages\pp-1.6.4-py2.7.egg\pp.py", line 149, in start
    self.pid = int(self.t.receive())
  File "D:\Anaconda2\lib\site-packages\pp-1.6.4-py2.7.egg\pptransport.py", line 140, in receive
    raise RuntimeError("Communication pipe read error")
RuntimeError: Communication pipe read error
通过调试器运行下一个pp示例时发生相同错误:

import math, sys, time
import pp
import os

def isprime(n):
    """Returns True if n is prime and False otherwise"""
    if not isinstance(n, int):
        raise TypeError("argument passed to is_prime is not of 'int' type")
    if n < 2:
        return False
    if n == 2:
        return True
    max = int(math.ceil(math.sqrt(n)))
    i = 2
    while i <= max:
        if n % i == 0:
            return False
        i += 1
    return True

def sum_primes(n):
    """Calculates sum of all primes below given integer n"""
    return sum([x for x in xrange(2,n) if isprime(x)])

print """Usage: python sum_primes.py [ncpus]
    [ncpus] - the number of workers to run in parallel,
    if omitted it will be set to the number of processors in the system
"""

print sys.argv
print sys.stdin
print sys.stdout
print sys.stderr

print 'read method'

#print 'str:', sys.stdin.read()
#print 'str:', sys.stdout.read()


print 'pptest print done'

# 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"



# Submit a job of calulating sum_primes(100) for execution.
# sum_primes - the function
# (100,) - tuple with arguments for sum_primes
# (isprime,) - tuple with functions on which function sum_primes depends
# ("math",) - tuple with module names which must be imported before sum_primes execution
# Execution starts as soon as one of the workers will become available
job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))

# Retrieves the result calculated by job1
# The value of job1() is the same as sum_primes(100)
# If the job has not been finished yet, execution will wait here until result is available
result = job1()

print "Sum of primes below 100 is", result

start_time = time.time()

# The following submits 8 jobs and then retrieves the results
inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
for input, job in jobs:
    print "Sum of primes below", input, "is", job()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()


# Parallel Python Software: http://www.parallelpython.com
导入数学、系统、时间
进口聚丙烯
导入操作系统
def iPrime(n):
“”“如果n为素数,则返回True;否则返回False”“”
如果不是isinstance(n,int):
raise TypeError(“传递给is_prime的参数不是'int'类型”)
如果n<2:
返回错误
如果n==2:
返回真值
max=int(math.ceil(math.sqrt(n)))
i=2
而我1:
ncpus=int(sys.argv[1])
#使用ncpus worker创建jobserver
作业\服务器=pp.server(ncpus,ppserver=ppserver)
其他:
#使用自动检测到的工作进程数创建jobserver
作业服务器=pp.server(ppservers=ppservers)
打印“启动pp”,作业\u服务器。获取\u ncpus(),“工人”
#提交计算和素数(100)的作业以供执行。
#sum_素数-函数
#(100,)-具有sum_素数参数的元组
#(isprime,)-具有函数和素数依赖的函数的元组
#(“math”,)-具有模块名称的元组,必须在执行sum_素数之前导入
#一旦其中一名工人可用,就开始执行
job1=job_server.submit(素数之和,(100,,(iPrime,,,(“数学”)等)
#检索由job1计算的结果
#job1()的值与sum_primes(100)相同
#如果作业尚未完成,执行将在此等待,直到结果可用
结果=作业1()
打印“低于100的素数之和为”,结果
开始时间=time.time()
#下面提交8个作业,然后检索结果
输入=(100000、100100、100200、100300、100400、100500、100600、100700)
作业=[(输入,作业服务器。提交(输入中输入的和素数,(输入,),(iPrime,),(“数学,))))
对于输入,作业中的作业:
打印“以下素数之和”,输入“是”,作业()
打印“经过的时间:”,Time.Time()-开始时间,“s”
job_server.print_stats()
#并行Python软件:http://www.parallelpython.com
似乎与缺少CLI有关


有人有解决办法吗?

我遇到了同样的问题,但无法解决根本原因。作为一种解决方法,在调试时,我在“import pp”之后添加了以下代码

class FakeServer(object):
    def __init__(self,ncpus,ppservers):
        pass
     def submit(self,fn,params,localfunctions=None, externalmodules=None):
         result = apply(fn,params)
         return lambda: result
 pp.Server = FakeServer

现在,您的代码是单线程的,易于调试,无需更改pp的用法。

真正的问题是并行python可以:

class PipeTransport(Transport):
    def __init__(self, r, w):
        if isinstance(r, types.FileType) and isinstance(w, types.FileType):
            self.r = r
            self.w = w
        else:
            raise TypeError("Both arguments of PipeTransport constructor " \
                    "must be file objects")
并且
sys.stdin
由调试器进行猴子补丁

一个本地修复程序正在将并行python更改为使用
sys.\uuu stdin.\uuuu
(由于这些检查,它还使用
sys.\uu stdout.\uuu
,因此,我想这是很自然的)

i、 e:将代码更改为:
self.t=pptransport.cpipertransport(sys.\uu stdin\uuuuu,sys.\uu stdout\uuuu)
——而不是当前的
self.t=pptransport.cpipertransport(sys.stdin,sys.\uu stdout\uuu)

ppworker.py
中(在
python3
python2
文件夹中,具体取决于您使用的python版本)


另请参见:调试器端的报告。

最小示例:导入pp;ppserver=();job_server=pp.server(ppserver=ppserver);打印“启动pp with”,作业\u服务器。获取\u ncpus(),“workers”;