Heinrich Schmetterling
Heinrich Schmetterling

Reputation: 6894

Python: set a function timeout without using signal or threads?

Is there a way to have a function raise an error if it takes longer than a certain amount of time to return? I want to do this without using signal (because I am not in the main thread) or by spawning more threads, which is cumbersome.

Upvotes: 2

Views: 901

Answers (1)

Amber
Amber

Reputation: 527478

If your function is looping through a lot of things, you could check the elapsed time during each iteration of the loop... but if it's blocked on something for the long period, then you need to have some other thread which can be handling the timing stuff while the thread you're timing is blocked.

Upvotes: 1

Related Questions