1

Today I've been working with wkhtmltopdf.exe in a web app and I was wondering whether I should use a lock (or the singleton pattern) in order to call Process.Start in a thread-safe manner.

My concern is that multiple users will do GETs simultaneously on the page that calls the exe file mentioned above. It's my understanding that each Request is created on its own thread, does this mean that calling an exe file (with Process.Start) is thread-safe in a web application?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Ulises
  • 13,229
  • 5
  • 34
  • 50

1 Answers1

2

Process.Start has nothing to do with thread safety. It is a thread safe call - you can call it as many times you want from as many threads you want. The problem that comes with this is that you are depending on an external process to do the job. Spawning multiple processes in an intensively used web application is not a good idea as you will be consuming more and more resources. So while this could be fine if your site doesn't have a big load, it is not recommended if you expect to start scaling.

Yeah, I know that converting HTML to PDF in a reliable and performant way which doesn't involve spawning processes and be happy with the final result costs money. But scalability of a web application usually comes at a cost.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • +1 I Appreciate your answer, just to clarify. Why is it thread safe then? is it because each request represents a thread, which in turn spawns another thread (`Process.Start`)? – Ulises Dec 14 '12 at 03:38
  • No, it's just the `Process.Start` method that is thread safe. When a method is thread safe this means that you could invoke this method in parallel from multiple threads at the same time. Each request represents a thread but Process.Start doesn't spawn new threads - it spawns, as it name suggests, new processes which are much more heavyweight than threads, they are containers for threads: http://en.wikipedia.org/wiki/Thread_(computing) – Darin Dimitrov Dec 14 '12 at 06:40