mirror of
				https://github.com/Brandon-Rozek/website.git
				synced 2025-11-03 23:01:13 +00:00 
			
		
		
		
	
		
			
				
	
	
	
	
		
			628 B
		
	
	
	
	
	
	
	
			
		
		
	
	
			628 B
		
	
	
	
	
	
	
	
| title | date | draft | tags | ||
|---|---|---|---|---|---|
| Quick Python: Seamless Variable Locks | 2020-05-06T23:01:39-04:00 | false | 
  | 
Combining Python's Getters and Setters with locks can give us seamless thread-safe variable access.
from threading import Lock
class Person:
    def __init__(self):
        self._age = 0
        self.age_lock = Lock()
    @property
    def age(self):
        a = None
        with self.age_lock:
            a = self._age
        return a
    
    @age.setter
    def age(self, a):
        with self.age_lock:
            self._age = a