Concurrency, I guess

Grant Edwards grante at visi.com
Mon Jan 23 15:52:38 EST 2006


On 2006-01-20, David S <davidschein at alumni.tufts.edu> wrote:

> Anyway, I have tried messing around with the threading module
> and referenced some examples I have come across, but to no
> avail.  Fundamentally I expected that when I change a global
> variable in 1 thread, the new value would be picked up in any
> other thread.  

Yup.

> This does not appear to be the case.

Works fine for me:

------------------------------8<------------------------------
import threading,time

def t1():
    global x
    for i in range(20):
        print "x =",x
        time.sleep(0.2)
        
def t2():
    global x
    for i in range(20):
        x += 1
        time.sleep(0.2)
        
x = 7        
        
threading.Thread(target=t1).start()
threading.Thread(target=t2).start()
------------------------------8<------------------------------

running the above program:

$ python te.py
x = 7
x = 8
x = 9
x = 10
x = 11
x = 12
x = 13
x = 14
x = 15
x = 16
x = 17
x = 18
x = 19
x = 20
x = 21
x = 22
x = 23
x = 24
x = 25
x = 26


-- 
Grant Edwards                   grante             Yow!  Xerox your lunch
                                  at               and file it under "sex
                               visi.com            offenders"!



More information about the Python-list mailing list