mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-01-22 23:11:53 +00:00
New Post
This commit is contained in:
parent
777acb461f
commit
ea27801577
1 changed files with 28 additions and 0 deletions
28
content/blog/pyseamlessvarlock.md
Normal file
28
content/blog/pyseamlessvarlock.md
Normal file
|
@ -0,0 +1,28 @@
|
|||
---
|
||||
title: "Quick Python: Seamless Variable Locks"
|
||||
date: 2020-05-06T23:01:39-04:00
|
||||
draft: false
|
||||
tags: ["python", "concurrency"]
|
||||
---
|
||||
|
||||
Combining Python's [Getters and Setters](https://brandonrozek.com/blog/pygetset/) with locks can give us seamless thread-safe variable access.
|
||||
|
||||
```python
|
||||
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
|
||||
```
|
||||
|
Loading…
Reference in a new issue