Skip to content
Advertisement

Python thread.join(timeout) not timing out

I am using threading python module. I want to execute a function which runs an expression entered by a user. I want to wait for it to finish execution or until a timeout period is reached. The following code should timeout after 5 second, but it never times out.

JavaScript

Why is this and how can I fix this behaviour? Should I try to use multiprocessing instead?

Advertisement

Answer

I suspect in this case you’re hitting an issue where the join can’t execute while the global interpreter lock is held by the very long-running calculation, which I believe would happen as a single atomic operation. If you change longFunc to something that happens over multiple instructions, such as a busy loop, e.g.

JavaScript

then it works as expected. Is the single expensive calculation realistic for your situation or did the example just happen to hit a very bad case?

Using the multiprocessing module does appear to resolve this issue:

JavaScript

This prints "end" as expected.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement