multi-threaded list update

Peter Hansen peter at engcorp.com
Tue Mar 22 23:40:39 EST 2005


Mike Rovner wrote:
> 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?

It's legal, it's safe.  The drawbacks are that your
routines don't know for certain they are operating
on lists, but could be operating on list subclasses
or even list-like objects (duck typing), which might
not have atomic appends.

Another drawback is that you might get into the habit
of taking "shortcuts" like this and, as your threaded
code gets more complex, you'll run into trouble with
race conditions or deadlocks or something because you
didn't develop good practices based around Locks,
Events, and most especially the Queue module. ;-)

(But if you can accept those drawbacks, keep doing
what you're doing.  Also consider searching the
archives for discussions involving the "dis" module
(google for "dis.dis" maybe?) and see how to learn
for yourself what is atomic and what's not.)

-Peter



More information about the Python-list mailing list