forked from inno-devops-labs/S24-core-course-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import fcntl | ||
from functools import wraps | ||
from threading import Lock | ||
from typing import Callable | ||
|
||
|
||
def increment(filename: str) -> None: | ||
with open(filename, 'rb+') as f: | ||
try: | ||
fcntl.flock(f.fileno(), fcntl.LOCK_EX) | ||
cur = int.from_bytes(f.read(), byteorder='little') | ||
f.seek(0) | ||
cur += 1 | ||
f.write(cur.to_bytes(byteorder='little', length=(cur.bit_length() // 8 + 1))) | ||
f.truncate() # Not necessary, as larger numbers take more bytes | ||
finally: | ||
# Note: will be unlocked anyway when the file is closed | ||
fcntl.flock(f.fileno(), fcntl.LOCK_UN) | ||
|
||
|
||
def increment_on_call(filename: str) -> Callable[[Callable, ...], Callable]: | ||
def decorator(func: Callable) -> Callable: | ||
@wraps(func) | ||
def with_increment(*args, **kwargs): | ||
increment(filename) | ||
return func(*args, **kwargs) | ||
return with_increment | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters