multi-threaded list update

M

Mike Rovner

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
 
P

Peter Hansen

Mike said:
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
 
M

Mike Rovner

Peter said:
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top