python - 我应该担心在多线程 python 脚本中并发访问字典吗?

标签 python multithreading dictionary concurrency

我想通过启动多个独立的异步操作线程来加快脚本的执行速度,否则这些线程会一个接一个地启动。

我用了the example from concurrent.future docs并将其改编为我的代码:

import concurrent.futures

def myfunc(elem):
    elem['ascii'] = ord(elem['name'])

mylist = [
    {'name': 'a'},
    {'name': 'b'},
    {'name': 'c'},
    {'name': 'd'},
    {'name': 'e'}
    ]

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    future_to_url = {executor.submit(myfunc, elem): elem for elem in mylist}
    for future in concurrent.futures.as_completed(future_to_url):
        try:
            future.result()
        except Exception as exc:
            print('error: '.format(exc))

print mylist

代码按预期工作,但我是否应该担心对 mylist 的并发访问,或者它是否被正确锁定并以串行方式访问(或任何正确的方式以便数据一致)?

在实际程序中,字典会更大,我想启动约 500 个 worker。

最佳答案

I would like to speed up the execution of a script by launching several threads

因为 challenges posed by CPython's implementation , 你应该使用 ProcessPoolExecutor相反,如果您对性能感兴趣。请注意,这将需要一种在工作人员如何与应共享的数据结构进行通信和/或交互方面更加复杂的设计。

现在,回到你的问题:

should I worry about the concurrent access to mylist or whether is is correctly locked and accessed in a serial way (or whatever is correct so that data is consistent)

list 将在多线程 环境中正常运行,但如果您在顶部有任何需要原子性的语义层,您将需要自己的锁定。举例来说,您的设计要求/期望 list 应始终包含七个元素,并且一些工作人员会执行 pop() 后跟 append() 。您将需要自己的锁来保护工作人员免受它们之间的并发访问。

关于python - 我应该担心在多线程 python 脚本中并发访问字典吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19962604/

相关文章:

python - 在 django 1.11 中添加没有类别的博客文章

python - 使用 Pylons 和 WMI 时出现 "Win32 exception occurred releasing IUnknown at..."错误

java - 当前正在运行的线程在 sleep 时是否持有锁?

c# - ConcurrentDictionary 添加或更新列表

python - 使用另一个数据框中匹配索引的值设置数据框列

Python异常KeyError问题

python - IronPython 启动速度非常慢

c - 为什么线程2要等待线程1结束?

算法或生成器或示例道路车道/和网络

swift - 如何从 Index 获取 Int 型索引?在 swift 词典中