![]() ![]() ![]() One way to make concurrency simpler is by using queues, and trying to have no other shared data structures. The threads are now deadlocked: no execution will proceed. Thread 2: Try to acquire L1, but it’s in use, so wait.Thread 1: Try to acquire L2, but it’s in use, so wait.Imagine you have two locks, L1 and L2, and one thread tries to acquire L1 followed by L2, whereas another thread tries to acquire L2 followed by L1. Locks introduce their own set of problems.įor example, you start having potential issues with deadlocks. Only one thread at a time can hold the lock, so only one increment happens at a time. ![]() lock = Lock () def increment ( self ): with self. The following counter, for example, will become corrupted if increment() is called from multiple threads:įrom threading import Lock class Counter ( object ): def _init_ ( self ): self. Without threads code is linear: line 2 is executed after line 1, with nothing happening in between.Īdd in threads, and now changes can happen behind your back. Writing programs with concurrency, programs with multiple threads, is hard. Join me, then, as I share this tale of woe. Rejoice when you hear the Python’s maintainers new response!.Weep when you read the response of Python’s maintainers!.Gasp as you realize that this bug is not theoretical!.Groan as reentrancy rears its ugly head!.Marvel as you discover how to debug deadlocks with gdb!.Frown at a mysteriously freezing program!.Shiver at the horror that is concurrent programming!.This story will take you on a veritable roller coaster of emotion and elucidation, as you: This is not a happy story: this is a tragedy, a story of deadlocks and despair. It’s also a story about a bug in Python’s Queue class, a class which happens to be the easiest way to make concurrency simple in Python. This is a story about how very difficult it is to build concurrent programs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |