473,382 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

multi-threaded list update

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
2 1664
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.