Ghostscript.NET Multithreading Issue

3k views Asked by At

The longest part of a monthly process we run is the automated slicing and conversion of some PDFs to images. Each PDF is read in, converted to 3 different PDFs, and those 3 are converted to images to be placed in e-mails to customers. The PDFs are unique per-customer, and we send a monthly PDF to at least 15,000 (frequently more like 22k) customers.

Our PDF generation and slicing is already multithreaded, but I've been looking into parallelizing the remaining bits of it.

To that end, I have converted our process to use Ghostscript.NET, which purports to be a library that supports parallelizing Ghostscript.

To that end, I wrapped this code in a Parallel.Foreach() loop, where each iteration through the loop works on a different initial PDF:

      GhostscriptVersionInfo gsVersionInfo = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);

      GhostscriptProcessor processor = null; 

        try
        {
            //sArgs is an array of arguments for ghostscript
            processor = new GhostscriptProcessor(gsVersionInfo, true);
            processor.StartProcessing(sArgs, new ConsoleStdIO(true,false,true));
            while (processor.IsRunning)
            {
                Thread.Sleep(100);
            }
        }

When I run the above code and force the Parallel.Foreach to use only 1 thread (disabling parallelization) it runs just like it used to and generates all the files correctly. If I use 5 degrees of parallelization, it begins throwing errors. These errors vary, but tend to indicate malformed input PDF files, making me think that the ghostscript processors aren't actually threadsafe, and are stepping on each other's inputs.

How do I correctly use Ghostscript.NET to run multiple instances of ghostscript, on different files, simultaneously?

1

There are 1 answers

0
amyn On BEST ANSWER

I am using Ghostscript.NET with multi-threading. The code you shared above should be inside the Parallel.For loop. yes the entire code which means starting from

GhostscriptVersionInfo gsVersionInfo = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);

till the end and it should work. As @KenS mentioned above, each multi-threaded process should use a different instance of GhostscriptProcessor.