Ghostscript.NET Multithreading Issue

南笙酒味 提交于 2019-12-10 23:07:51

问题


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:


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.



来源:https://stackoverflow.com/questions/26917330/ghostscript-net-multithreading-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!