Skip to content
Snippets Groups Projects
Commit 3e782aea authored by Anton Tetov Johansson's avatar Anton Tetov Johansson
Browse files

tooling

parent 761ab035
No related branches found
No related tags found
No related merge requests found
import threading
class AtomicCounter:
"""An atomic, thread-safe counter.
From:
https://gist.github.com/benhoyt/8c8a8d62debe8e5aa5340373f9c509c7#gistcomment-3142969
>>> counter = AtomicCounter()
>>> counter.inc()
1
>>> counter.inc(num=4)
5
>>> counter = AtomicCounter(42.5)
>>> counter.value
42.5
>>> counter.inc(num=0.5)
43.0
>>> counter = AtomicCounter()
>>> def incrementor():
... for i in range(100000):
... counter.inc()
>>> threads = []
>>> for i in range(4):
... thread = threading.Thread(target=incrementor)
... thread.start()
... threads.append(thread)
>>> for thread in threads:
... thread.join()
>>> counter.value
400000
"""
def __init__(self, initial=0):
"""Initialize a new atomic counter to given initial value"""
self._value = initial
self._lock = threading.Lock()
def inc(self, num=1):
"""Atomically increment the counter by num and return the new value"""
with self._lock:
self._value += num
return self._value
def dec(self, num=1):
"""Atomically decrement the counter by num and return the new value"""
with self._lock:
self._value -= num
return self._value
@property
def value(self):
return self._value
if __name__ == "__main__":
import doctest
doctest.testmod()
name: abb-egm-examples
name: abb-egm-examples1
channels:
- conda-forge
dependencies:
- python >=3.9, <3.10
- numpy
- protobuf
......@@ -4,3 +4,6 @@ requires = [
"wheel",
]
build-backend = "setuptools.build_meta"
[tool.isort]
profile = "black"
......@@ -4,6 +4,16 @@ version = 0.1.0
[options]
packages = abb_egm_client
python_requires = >= 3.8
python_requires = >= 3.9
install_requires =
protobuf
[options.extras_require]
dev =
black
flake8
isort >= 5.0.0
[flake8]
max-line-length = 88
extend-ignore = E203
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment