Python, counter atomic increment
How can I translate the following code from Java to Python?
AtomicInteger cont = new AtomicInteger(0);
int value = cont.getAndIncrement();
Most likely with an threading.Lock
around any usage of that value. There's no atomic modification in Python unless you use pypy (if you do, have a look at __pypy__.thread.atomic
in stm version).
itertools.count
returns an iterator which will perform the equivalent to getAndIncrement()
on each iteration.
Example:
import itertools
cont = itertools.count()
value = next(cont)