Skip to content

magick-based function hangs when used in mclapply #407

@zeileis

Description

@zeileis

Summary

I have a magick-based function that essentially converts PDF to PNG files. I'm using that function via parallel::mclapply which, in principle, works fine. However, if I call it first outside of mclapply then the subsequent application in mclapply hangs and does not proceed.

Minimal reproducible example

First, I generate four PDF files for illustration:

fil <- paste0(LETTERS[1:4], ".pdf")
for(i in fil) { pdf(i); plot(1:10); dev.off() }

The magick-based function to_png can be used to convert a PDF file to a PNG file:

to_png <- function(x) {
  magick::image_read(x) |>
    magick::image_convert("png") |>
    magick::image_write(path = gsub("pdf", "png", x), format = "png")
}

Using that function sequentially works fine, as expected:

for(i in fil) to_png(i)

However, if I do the analogous operation on four cores, the to_png() function hands and does not proceed:

library("parallel")
mclapply(fil, to_png, mc.cores = 4)

Curiously, several slight adaptations work:

  • In a fresh session when I run the mclapply first (before the sequential for() version), then it works.
  • With mc.cores = 1 it works.

Is there anything I can - or should - do to avoid this problem?

This is in Debian GNU/Linux with R 4.4.1 and magick 2.8.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions