多个python线程同时写入同一列表中的不同记录-可以吗? [英] Multiple python threads writing to different records in same list simultaneously - is this ok?

查看:79
本文介绍了多个python线程同时写入同一列表中的不同记录-可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复多个线程正在向内存中的列表写入错误.现在,我有一个线程锁,偶尔会遇到与线程中完成的工作有关的问题.

I am trying to fix a bug where multiple threads are writing to a list in memory. Right now I have a thread lock and am occasionally running into problems that are related to the work being done in the threads.

我希望简单地对列表进行散列,每个线程一个,然后删除线程锁.似乎每个线程都可以写自己的记录而不必担心其他线程,但是也许它们都使用相同的拥有哈希值这一事实本身就是一个问题.

I was hoping to simply make an hash of lists, one for each thread, and remove the thread lock. It seems like each thread could write to its own record without worrying about the others, but perhaps the fact that they are all using the same owning hash would itself be a problem.

有人碰巧知道这是否行得通吗?如果没有,例如,我可以为每个线程动态地将列表添加到包中吗?这本质上是一回事吗?

Does anyone happen to know if this will work or not? If not, could I, for example, dynamically add a list to a package for each thread? Is that essentially the same thing?

我不是线程专家,所以欢迎任何建议.

I am far from a threading expert so any advice welcome.

谢谢

推荐答案

import threading

def job(root_folder,my_list):
    for current,files,dirs in os.walk(root):
        my_list.extend(files)
        time.sleep(1)

my_lists = [[],[],[]]
my_folders = ["C:\\Windows","C:\\Users","C:\\Temp"]
my_threads = []
for folder,a_list in zip(my_folders,my_lists):
    my_threads.append(threading.Thread(target=job,args=(folder,a_list)
for thread in my_threads:
   thread.start()
for thread in my_threads:
   thread.join()

my_full_list = my_lists[0] + my_lists[1] + my_lists[2]

这样,每个线程仅修改其自己的列表,最后将所有单独的列表合并在一起

this way each thread just modifies its own list and at the end combines all the individual lists

还指出,这将使性能提升为零(实际上可能比不进行线程化要慢...),而使用多处理可能会带来性能提升...

also as pointed out this gives zero performance gain (actually probably slower than not threading it... ) you may get performance gains using multiprocessing instead ...

这篇关于多个python线程同时写入同一列表中的不同记录-可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆