0

I have written a code to crunch some data on Azure server. The first time that I wrote the code, it was linear For Each loop and would take forever to be finished, I improved the code by using Parallel as it follows :

Parallel.ForEach(users, currentUser =>
  {
     int number = myrollup.CountFilmsForUsers(currentUser.UserId,distort,tend).Result;
     lock(myrollup)
      {
        var record = new UserAnalytics();
        { 
          UserId = currentUser.UserId,
          UserFName = currentUser.FirstName,
          UserLName = currentUser.LastName,
         };
        session.SaveOrUpdate(record);
       }
    });

This parallel loop does the job in 9 min, however I want to improve this more by using a Producer / Consumer pattern. Any ideas on how to do this with Nhibernate in C#?

Blachshma
  • 17,097
  • 4
  • 58
  • 72
Payam
  • 741
  • 3
  • 14
  • 37
  • 3
    Doing something in parallel then guarding most of the loop's body with a lock doesn't make it very parallel. – vcsjones Jan 04 '13 at 22:08
  • I know that this is not as Parallel as I want because Nhibernate is not thread safe. That is why I want to use the Producer / Consumer pattern to make one thread to just read and one thread to write data to my SQL DB. – Payam Jan 04 '13 at 22:10
  • http://msdn.microsoft.com/en-us/library/dd267312.aspx – Robert Harvey Jan 04 '13 at 22:16
  • http://stackoverflow.com/search?q=producer+consumer+queue – Robert Harvey Jan 04 '13 at 22:16
  • I suggest you find an article that explains producer-consumer algorithms with the TPL and .NET in general. That will help you more that having the community produce the code for you. – usr Jan 04 '13 at 23:09

1 Answers1

0

I definitely is not a good idea to share one NHibernate session between multiple threads of execution. Try to use nhibernate profiler to find a bottleneck or switch to StatelessSession instead

Akim
  • 8,469
  • 2
  • 31
  • 49