Generation of pdf in multiple threads, task or parallelFor

Alexi Tomala 0 Reputation points
2023-07-19T03:06:04.1666667+00:00

Hello, I thank you in advance for your support.

I am improving a process of generating a pdf file.

The process currently is sequential and takes a long time to generate

The ephtmltopdf library is used with a method that calls an .aspx page and returns an array of bytes to the client.

To improve the times, the functionality was changed to be through threads, task, parallelFor, but if I succeed.

It should be noted that the current process takes less than or equal to one second to respond.

This development is on a web service .asmx and also on wcf in C#.

Any suggestions why the parallel execution doesn't succeed?

I attach an example graph:

task

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,364 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

2 answers

Sort by: Most helpful
  1. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-07-19T08:17:40.7033333+00:00

    Hi @Alexi Tomala

    In fact, I think Asynchronous file access might help you.

    • Asynchrony makes UI applications more responsive because the UI thread that launches the operation can perform other work. If the UI thread must execute code that takes a long time (for example, more than 50 milliseconds), the UI may freeze until the I/O is complete and the UI thread can again process keyboard and mouse input and other events.
    • Asynchrony improves the scalability of ASP.NET and other server-based applications by reducing the need for threads. If the application uses a dedicated thread per response and a thousand requests are being handled simultaneously, a thousand threads are needed. Asynchronous operations often don't need to use a thread during the wait. They use the existing I/O completion thread briefly at the end.
    • The latency of a file access operation might be very low under current conditions, but the latency may greatly increase in the future. For example, a file may be moved to a server that's across the world.
    • The added overhead of using the Async feature is small.
    • Asynchronous tasks can easily be run in parallel.

    Asynchronous file access (C#)

    You can use asynchronous operations in the WCF service section to achieve the functionality you want.

    How to: Call WCF Service Operations Asynchronously

    Another thing I don't understand is that your feature is HTML to PDF. Is it uploading an HTML file or currently converting the current page?Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 61,731 Reputation points
    2023-07-19T15:27:03.1933333+00:00

    there is extra overhead in running parallel tasks. if running the tasks in parallel takes longer than serially, then the tasks are blocking each other. you will need to use a profiler to determine the blocking factor. for example running the db queries in parallel may slow them down (say they are doing scans and more disk seeks are required).

    note: parallel for is handy for limiting number of parallel tasks. say you want to loop thru 100 items, but only want 5 running in parallel at time. parallel for handles this case.

    0 comments No comments