Directly calling threaded class instance methods and attributes

Just just at xs4all.nl
Thu Oct 28 14:08:28 EDT 2004


In article <mailman.5624.1098986746.5135.python-list at python.org>,
 Josiah Carlson <jcarlson at uci.edu> wrote:

> >>> val2 = 0
> >>> lock = threading.Lock()
> >>> def goo(n):
> ...     global val2, lock
> ...     for i in xrange(n):
> ...         lock.acquire()
> ...         val2 += 1
> ...         lock.release()
> ...
> >>> for i in xrange(10):
> ...     threading.Thread(target=goo, args=(100000,)).start()
> ...
> >>> #wait...
> ...
> >>> val2
> 1000000

FWIW, you don't need a global statement for globals you don't assign to, 
so you don't need to declare lock global.

Just



More information about the Python-list mailing list