reentrant
Code is reentrant when one copy of it can be interrupted partway through, called again from the start, and complete that second call correctly before the first one continues. This works because each call keeps its own working data, so two calls that overlap in time never overwrite each other’s values.
Reentrancy matters whenever the same code can be entered again before an earlier call finishes. A recursive function calls itself, a signal handler can interrupt a function and call it again, and two threads can run the same function at once.
To stay correct in these situations, reentrant code keeps its data in local variables and parameters instead of global or static storage. It also never rewrites its own instructions and calls only other reentrant code.
Related Resources
Tutorial
Python Thread Safety: Using a Lock and Other Techniques
In this tutorial, you'll learn about the issues that can occur when your code is run in a multithreaded environment. Then you'll explore the various synchronization primitives available in Python's threading module, such as locks, which help you make your code safe.
For additional information on related topics, take a look at the following resources:
- An Intro to Threading in Python (Tutorial)
- Speed Up Your Python Program With Concurrency (Tutorial)
- Thinking Recursively in Python (Tutorial)
- Recursion in Python: An Introduction (Tutorial)
- Thread Safety in Python: Locks and Other Techniques (Course)
- Python Thread Safety: Using a Lock and Other Techniques (Quiz)
- Threading in Python (Course)
- Python Threading (Quiz)
- Speed Up Python With Concurrency (Course)
- Python Concurrency (Quiz)
- Thinking Recursively With Python (Course)
- Recursion in Python (Course)
- Recursion in Python: An Introduction (Quiz)