如何加快多处理python

时间:2018-12-30 09:57:09

标签: python multiprocessing python-imaging-library mandelbrot

我正在尝试生成Mandelbrot Set图像,但是该图像需要一段时间。代码很烦人,所以如果您可以帮助我加快速度,那将非常有帮助。谢谢。请注意,代码是从我的IDE编译并运行的。我是多处理的新手,所以请为我做些哑巴。

def daemon_summoner(r):
'''allows code to be restarted with different file if crash during super high res generation'''
t = Thread(target=process_job(r))
t.start()


def process_job(r):
    """creates a slice of the set"""
    img = Image.new('1', size, color)  # B&W image
    print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
    r = r * 100
    for rval in range(100):
        for i in range(-resolution, resolution, 1):
            if abrot(((r + rval) / resolution), (i / resolution * 1j)):
                try:
                    img.putpixel((rval, i + resolution,), 0)
                except:
                    print(r, rval, i + resolution)
img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


def abrot(x, y):
    """tests a point"""
    c = (x + y)
    z = 0 + 0j
    for _ in range(5):
        z = z * z + c
    if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
        return False
    for _ in range(int(resolution / 10)):
        if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
            return True
        z = z * z + c
        if abs(real(z)) >= 2:
            return False
    return True

1 个答案:

答案 0 :(得分:0)

Mandelbrot迭代z * z + c可能需要数十亿次操作才能生成单个图像,因此加快其速度的第一步是删除iterate_once函数并在线完成工作。