By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

multi-threaded list update

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
Peter Hansen wrote:
(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.)


Thanks, Peter. I googled groups on 'python dis.dis atomic' and it was
all on target and quite interesting.

Mike

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.