File: index.md

package info (click to toggle)
python-fasteners 0.19-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: python: 1,626; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# User Guide

## Basic Exclusive Lock Usage

Exclusive Lock for independent processes has the same API as the
[threading.Lock](https://docs.python.org/3/library/threading.html#threading.Lock)
for threads:
```python
import fasteners
import threading

lock = threading.Lock()                                 # for threads
lock = fasteners.InterProcessLock('path/to/lock.file')  # for processes

with lock:
    ... # exclusive access

# or alternatively    

lock.acquire()
... # exclusive access
lock.release()
```

## Basic Reader Writer lock usage

Reader Writer lock has a similar API, which is the same for threads or processes:

```python
import fasteners

# for threads
rw_lock = fasteners.ReaderWriterLock()                                 
# for processes
rw_lock = fasteners.InterProcessReaderWriterLock('path/to/lock.file')  

with rw_lock.write_lock():
    ... # write access

with rw_lock.read_lock():
    ... # read access

# or alternatively, for processes only:

rw_lock.acquire_read_lock()
... # read access
rw_lock.release_read_lock()

rw_lock.acquire_write_lock()
... # write access
rw_lock.release_write_lock()
```

## Advanced usage

For more details and options see [Process lock details](inter_process.md) and [Thread lock details](inter_thread.md).