473,396 Members | 1,998 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,396 software developers and data experts.

dictionaries and threads

Hi,

I know the Global Interpreter Lock ensures that only one python thread
has access to the interpreter at a time, which prevents a lot of
situations where one thread might step on another's toes.

But I'd like to ask about a specific situation just to be sure I
understand things relative to some code I'm writing.

I've got a dictionary which is accessed by several threads at the same
time (that is, to the extent that the GIL allows). The thing is,
however, no two threads will ever be accessing the same dictionary
items at the same time. In fact the thread's ID from thread.get_ident()
is the key to the dictionary; a thread only modifies items
corresponding to its own thread ID. A thread will be adding an item
with its ID when it's created, and deleting it before it exits, and
modifying the item's value in the meantime.

As far as I can tell, if the Python bytecodes that cause dictionary
modifications are atomic, then there should be no problem. But I don't
know that they are because I haven't looked at the bytecodes.

Any feedback on this would be appreciated. For various reasons, we're
still using Python 2.3 for the time being.

Gary

--

Gary Robinson
CTO
Emergent Music, LLC
gr*******@goombah.com
207-942-3463
Company: http://www.goombah.com
Blog: http://www.garyrobinson.net
Jul 19 '05 #1
1 1217
Gary Robinson <gr*******@goombah.com> writes:
As far as I can tell, if the Python bytecodes that cause dictionary
modifications are atomic, then there should be no problem. But I don't
know that they are because I haven't looked at the bytecodes.


Depending on behavior like that is asking for trouble. If it doesn't
kill your app's performance, put some kind of locks around the
dictionary and/or direct all access to the directory through a single
thread. The favorite Python idiom for that seems to be the Queue
module; you'd set up a work queue and a result queue to communicate
with the thread controlling the dictionary.

If your app absolutely can't stand that, look to a more fundamental
solution, maybe something like POSH (poshmodule.sf.net).
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
57
by: Chris Foote | last post by:
Hi all. I have the need to store a large (10M) number of keys in a hash table, based on a tuple of (long_integer, integer). The standard python dictionary works well for small numbers of keys,...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.