multi-threaded list update

Mike Rovner mrovner at propel.com
Wed Mar 23 04:21:19 CET 2005


Hello,

Please advise on multi-threaded list *append*:

import time, random, thread

aList = []

def main():
   for i in range(10):
     thread.start_new_thread(updater, (i,))
   time.sleep(30)
   print aList

def updater(n):
   global aList
   time.sleep( random.randint(1,n+1) )
   aList.append(n)

if __name__=='__main__':
   main()

I rely on GIL and believe that .append is atomic operation.
It that legal? What are the drawbacks?

Thanks,
Mike




More information about the Python-list mailing list