New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread implementation #5
Comments
Hi, good question. When dealing with multiple threads, the disaster situation we want to avoid is having two threads edit the state of the same object at the same time. That can lead to bizarre, hard to diagnose problems. Using the example code you pasted, if two threads each had access to the same So the problem we're trying to solve with these locks is all on the write end. On the read side, in my experience Python doesn't care if one thread is reading a property while another thread is updating it. If both a read and write happen simultaneously, I imagine Python sends the old but complete value to the reader, but I honestly don't know the specifics on that. In our bot application, it doesn't really matter if our detector gets a slightly old screenshot from WindowCapture. That is, if the main thread calls Sharing locks between threads might be needed in some situations, but then you're losing a lot of the advantage of running separate threads because you're bringing them back into synchronization. You could have the Detection object wait for the WindowCapture to finish taking the next screenshot by calling If you are in a situation where you need to lock both the writes and the reads, here's a good example of how to do that: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch06s04.html |
I see! If I had to guess since the screenshot is medium size array it cannot be updated all at once by a thread, so reading a partially updated screenshot can happen. Still, it has no impact if you are building a bot like this one. |
Hi, first of all thanks for the series, it is great and really clear.
One question regarding how threads work.
When, for example, a Detector instance calls the update(self, screenshot) method:
Shouldn' t the lock acquired by the method be the one of the WindowCapture instance? I don't understand how threads are talking to each other if the locks they acquired are the ones enclosed in the objects each of them are supposed to manage.
Thanks in advance!
The text was updated successfully, but these errors were encountered: