8000 [Bug]: savefig in a thread pool when another thread pool is working is more slower than matplotlib3.2.0 · Issue #21816 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: savefig in a thread pool when another thr 8000 ead pool is working is more slower than matplotlib3.2.0 #21816
Closed
@wqh17101

Description

@wqh17101

Bug summary

there are two thread pools working together.
savefig is in the one of them.
With matplotlib 3.2.0 it costs 1s.
With matplotlib 3.4.3 it costs 30s.

Code for reproduction

import time
from concurrent.futures import ThreadPoolExecutor, as_completed

import matplotlib
from matplotlib.figure import Figure

matplotlib.use('agg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches

POOL_1 = ThreadPoolExecutor(3)
POOL_2 = ThreadPoolExecutor(3)


def _pull_stream():
    while True:
        a = [100]
        if a[0] < 200:
            continue


def generate_empty():
    fig = Figure(figsize=(9.6, 12.8))
    fig.savefig("./test.jpg")
    fig.clear()


def draw_process():
    start_time = time.time()
    img = plt.imread("./test.jpg")
    fig = plt.figure(figsize=(9.6, 12.8))
    ax_var = plt.Axes(fig, [0., 0., 1., 1.])
    ax_var.set_axis_off()
    ax_var.imshow(img, aspect="equal")
    fig.add_axes(ax_var)
    for i in range(3):
        for j in range(10):
            ax_var.add_patch(patches.Rectangle((1, 1), 2, 3))
            ax_var.text(4, 0, 'Here you are', family='fantasy', fontsize=12, style='italic', color='mediumvioletred')
    plt.axis('off')
    plt.savefig("./test_res.jpg")
    plt.close('all')
    return "cost {}s".format(time.time() - start_time)


def run():
    POOL_1.submit(_pull_stream)
    all_task = [POOL_2.submit(draw_process) for i in range(10)]
    for future in as_completed(all_task):
        data = future.result()
        print("in main: get page {}s success".format(data))


if __name__ == '__main__':
    generate_empty()
    run()

Actual outcome

i just write a simple draw process, for my real process i use add_patch(Rectangle) and text to add some elements for my pic.

matplotlib 3.2.0
image
matplotlib3.4.3
image

Expected outcome

get the same speed as 3.2.0

Additional information

No response

Operating system

No response

Matplotlib Version

3.4.3

Matplotlib Backend

agg

Python version

3.8.8

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0