<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">As long as Python uses a GIL to protect C level function<br>
calls, you can use an iterator for this:<br>
<br>
import itertools<br>
x = itertools.count()<br>
...<br>
mycount = next(x)<br>
</blockquote><div><br></div><div>Yeah, that's a neat hack -- I saw it recommended on StackOverflow, and saw it used in the standard library somewhere. I think that's probably okay in the *CPython* stdlib, because it's CPython so you know it has the GIL. But this wouldn't work in other Python implementations, would it (IronPython and Jython don't have a GIL). Or when itertools.count() is implemented in pure Python on some system? Seems like it could blow up in someone's face when they're least expecting it. I also think using *iter*tools is a pretty non-obvious way to get a thread-safe counter.</div><div><br></div><div>-Ben</div><div><br></div></div></div></div>