resourcecap 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: resourcecap
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: A python package to limit the resource usage of any program
5
5
  Author: purushottam-dafure
6
6
  Author-email: purushottam-dafure <purushottamdafure@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "resourcecap"
3
- version = "0.2.0"
3
+ version = "0.2.1"
4
4
  description = "A python package to limit the resource usage of any program"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -17,9 +17,6 @@ classifiers = [
17
17
  name = "purushottam-dafure"
18
18
  email = "purushottamdafure@gmail.com"
19
19
 
20
- [project.scripts]
21
- resourcecap = "resourcecap:main"
22
-
23
20
  [build-system]
24
21
  requires = ["uv_build>=0.12.17,<0.13.0"]
25
22
  build-backend = "uv_build"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "resourcecap"
3
- version = "0.2.0"
3
+ version = "0.2.1"
4
4
  description = "A python package to limit the resource usage of any program"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -16,9 +16,6 @@ classifiers = [
16
16
  "Typing :: Typed",
17
17
  ]
18
18
 
19
- [project.scripts]
20
- resourcecap = "resourcecap:main"
21
-
22
19
  [build-system]
23
20
  requires = ["uv_build>=0.12.17,<0.13.0"]
24
21
  build-backend = "uv_build"
@@ -2,8 +2,4 @@ from ._budget import Budget, BudgetExhaustedError
2
2
  from ._costs import costs
3
3
  from ._resources import Spendable
4
4
 
5
- __all__ = ["Budget", "BudgetExhaustedError", "Spendable", "costs", "main"]
6
-
7
-
8
- def main() -> None:
9
- print("Hello from resourcecap!")
5
+ __all__ = ["Budget", "BudgetExhaustedError", "Spendable", "costs"]
@@ -1,5 +1,6 @@
1
1
  import contextvars
2
2
  import logging
3
+ import threading
3
4
  from collections.abc import Hashable, Mapping
4
5
  from types import TracebackType
5
6
 
@@ -39,6 +40,7 @@ class Budget:
39
40
  ) -> None:
40
41
  self.limits = merge_resources(amount, limits)
41
42
  self.spent: dict[Hashable, float] = {}
43
+ self._lock = threading.Lock()
42
44
  self._token: contextvars.Token[tuple[_tracking._Chargeable, ...]] | None = None
43
45
 
44
46
  def exhausts_at_start(self, key: Hashable) -> bool:
@@ -53,14 +55,15 @@ class Budget:
53
55
  (spend charged against it so far), and `remaining` (`allocated
54
56
  - used`, which can go negative for a `warn_only` resource).
55
57
  """
56
- return {
57
- key: {
58
- "allocated": limit.amount,
59
- "used": self.spent.get(key, 0.0),
60
- "remaining": limit.amount - self.spent.get(key, 0.0),
58
+ with self._lock:
59
+ return {
60
+ key: {
61
+ "allocated": limit.amount,
62
+ "used": self.spent.get(key, 0.0),
63
+ "remaining": limit.amount - self.spent.get(key, 0.0),
64
+ }
65
+ for key, limit in self.limits.items()
61
66
  }
62
- for key, limit in self.limits.items()
63
- }
64
67
 
65
68
  def charge(self, key: Hashable, amount: float) -> None:
66
69
  """Record spend against this budget's limit for `key`.
@@ -73,8 +76,10 @@ class Budget:
73
76
  if limit is None:
74
77
  return
75
78
 
76
- spent = self.spent.get(key, 0.0) + amount
77
- self.spent[key] = spent
79
+ with self._lock:
80
+ spent = self.spent.get(key, 0.0) + amount
81
+ self.spent[key] = spent
82
+
78
83
  if spent > limit.amount:
79
84
  if limit.warn_only:
80
85
  logger.warning(
@@ -86,7 +91,8 @@ class Budget:
86
91
  )
87
92
 
88
93
  def __enter__(self) -> "Budget":
89
- self.spent = {}
94
+ with self._lock:
95
+ self.spent = {}
90
96
  self._token = _tracking.push(self)
91
97
  return self
92
98
 
File without changes