0

I have a nested for loop. I am trying to implement a function on a numpy array. Sadly, some data point are bad and start taking ram till system freezes. But, i can figure out those faulty indexes manually by interrupting the loop and removing that data.

The the best thing i can think of is executing a time bound 'for' loop where the loop exists when execution time exceeds, say 2 seconds. here is a sample code for my implementation.

channel=6
for index in range(len(X_train)):
      for i in range(channel):
               X_train[index][:,channel] = function_that_creates_issue_for_some_index_values(X_train[index][]......)
  • Does [this](https://stackoverflow.com/questions/40915527/kill-function-after-a-given-amount-of-time) help? – gimix May 26 '22 at 10:22
  • 1
    Does this answer your question? [Timeout on a function call](https://stackoverflow.com/questions/492519/timeout-on-a-function-call) – Jacob May 26 '22 at 12:49

2 Answers2

1

If a function_that_creates_issue_for_some_index_values() is stuck, it's two-way to break this

  1. Add timeout mechanism in the function_that_creates_issue_for_some_index_values() and break it from the inside

  2. Start for index in range(len(X_train)): loop as multiprocessing. Then you can start another process of monitoring that loop and break it when it gets stuck.

If only this loop get stuck for i in range(channel): not function_that_creates_issue_for_some_index_values(), just use @Othmane Messaoud solution

Moonar
  • 141
  • 6
0

Try this:

import time
channel=6
for index in range(len(X_train)):
      start_time = time.clock()
      for i in range(channel):
          if (time.clock() - start_time > 2 ) :
              break;