0

I am using OmniThread Parallel.foreach(). There are instances where the loop takes a long time or gets stuck.

I would like to know, is it possible to timeout each process in the Parallel.foreach() loop?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
James
  • 41
  • 4
  • 1
    Depends. If by stuck you mean a deadlock, that's a different question from just taking a long time, but would still complete. Which is it. – David Heffernan Jun 22 '20 at 07:25
  • @DavidHeffernan thanks for your response In the instance that there is a lock or an error in the DB which prevents it from continuing. – James Jun 22 '20 at 07:36
  • In that case the only way to handle this is to put the task which can block into a separate process and use IPC to communicate between your process and the process that can block. If it blocks, you can terminate the process. Only processes have sufficient isolation of resources to do this. Threads don't. – David Heffernan Jun 22 '20 at 07:38
  • 2
    Most databases have timeout settings available in the connection parameters or even as code / query options. There are also options on how long to wait for locks - you can often even have it fail instantly instead of waiting to try and get a lock. For example on SQL Server using Lock_Timeout with your own retry logic. – Brian Jun 23 '20 at 08:36

1 Answers1

0

In short: Nope, there isn't.

Unless you program the timeout handling in your 'thread body' code (what gets called in the execute).

eg my database engine allows sending a CancelProcessing call to running queries from a different thread that runs the query, this would 'cleanly' end the running subthread.

'Dirty' end of the subthreads:

I added a FR to Omnithread's github site to add an (Dirty) Terminate method to the IOmniParallel interfaces (and alikes). Which has is drawback because killing subthreads will probably leave you with memory/resource leaks.

Meanwile you might use this dirty shutdown solution/workaround wich actually comes down fixing a similar problem (I had a deadlock in my parallel processed routine, so my parallel.Waitfor never returned true, and worse my IOmniParallelTask interface variable was never released causing the calling thread to block as well.

H.Hasenack
  • 1,094
  • 8
  • 27