开发者

Possible error reading data from different threads

I currently program a program which uses multiple threads. To be able to share data across these threads I use Locks like threading.Lock to avoid access problem during run.

But the problem that I got is that I have to create huge amount locks for every data just for reading that data. Even if I 'group' data and use for them the same lock there are too many of them.

So I am asking myself: If a thr开发者_开发技巧ead just reads data and does not changes it (that also applies to all other threads), are there any problems which can occur?

Thanks in advance,


If all your threads just read data, there is no problem accessing the data simultaneously from different threads, as long as reading the data does not change it. Changing the read data can occur in some data structure, defaultdict being a tricky example.

If you have a consumer\producer scenario, consider using Queue.Queue, a thread-safe data structure which allows simultaneous reads and writes. From the Python documentation:

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

for item in source():
    q.put(item)

q.join()       # block until all tasks are done


Suppose that you have 50 threads. As long as a single thread is able to write data, then any other thread that reads data can create a problem.

For instance, a thread writes data and then another reads that data. If there is a concurrency problem, it would either read data before or after the change. You wouldn't be able to recognize that.

Only if EVERY thread just reads, you will not have a problem.


I believe you only have to lock if you write that means that if you have 20 threads reading a only one writing then you only have to lock when the writing one is working reducing 21 locking threads to only one

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜