Dictionary inserts and threads

M

Martin P. Hellwig

Hi all,

I'm thinking to speed up a process, I like to use multiple threads to
get data fractions from multiple servers and place those data fragments
into a local dictionary for further processing, the dictionary will look
like this:

self.dic = {'thread_a':dict(),
'thread_b':dict()}

The fragments will not overlap each other, since they will be
subdictionaries within the dictionary, though it would be possible that
multiple threads do something like this at the same time:

self.dic['thread_a'] = data_from_a()
and
self.dic['thread_b'] = data_from_b()

I assume that this is not a problem, but since assuming something gives
a lot of room for screw-ups I rather ask beforehand if I should except
all kinds of troubles when multiple threads update 'at the same time' a
dictionary or that I should lock the dictionary, for each update?

Thanks for your insights.
 
P

Paul Rubin

Martin P. Hellwig said:
I assume that this is not a problem, but since assuming something
gives a lot of room for screw-ups I rather ask beforehand if I should
except all kinds of troubles when multiple threads update 'at the same
time' a dictionary or that I should lock the dictionary, for each
update?

Use a lock, or serialize the updates through a queue.
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top