不同线程访问相同字典(只读)
我有一个函数(例如 myFunc),它需要开始和结束日期&对其进行迭代并访问一些字典来处理该时间段的数据。我希望使用线程来并行化此代码。
我的 main() 函数从一堆文件中读取数据&将它们存储在字典中。每次我调用 myFunc 时,我都会传递这些字典和 start & 。结束日期。当我使用多个线程调用 myFunc 并传递相同的字典时,会发生冲突吗?这些词典仅用于只读目的。
myFunc 创建许多字典,然后将其写入另一个文件。每个线程的输出文件都会有所不同,但由于所有线程中的字典名称都相同,是否会发生冲突?从理论上讲,我理解线程的概念意味着只要这些字典不是全局的,就不应该发生冲突。但由于这是我第一次尝试线程,需要一些确认。
谢谢
I have a function (myFunc for e.g.) that takes a start and end date & iterates over it and accesses some dictionaries to process the data for this time period. I am looking to use threads to parallelize this code.
My main() function reads in data from a bunch of files & stores them in dictionaries. Every time I call myFunc I pass these dictionaries and a start & end date. When I call myFunc using multiple threads and pass along the same dictionaries, would there be a conflict? These dictionaries are used only for read-only purposes.
myFunc creates a lot of dictionaries which it then writes out to another file. This output file would differ for every thread, but since across all the threads the dictionaries are named the same, would there be a conflict? Theoretically, I understand that the concept of threading means that there should not be a conflict as long as those dictionaries are not global. But since its my first attempt at threading, would like some confirmation.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用线程,不,你不会有问题。您也不会实现并行性。 Python中的全局解释器锁(GIL)在任何时候都只允许单个线程进行处理。如果您想要真正的并行处理,您需要查看多处理模块。它能够实际利用机器上的多个处理器。
Using threading, no, you're not going to have a problem. You're also not going to achieve parallelism. The Global Interpreter Lock (GIL) in Python only allows a single thread to process at any time. If you want true parallel processing you need to look at the multiprocessing module. It has the ability to actually leverage multiple processors on a machine.