不同线程访问相同字典(只读)

发布于 2024-12-08 16:40:56 字数 363 浏览 2 评论 0原文

我有一个函数(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-15 16:40:56

使用线程,不,你不会有问题。您也不会实现并行性。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文