python中的多线程处理是无用的。最好使用多处理模块。因为多线程只能在较少的情况下发挥积极作用。在Python implementation detail: In CPython, due to the Global
Interpreter Lock, only one thread can execute Python code at once
(even though certain performance-oriented libraries might overcome
this limitation). If you want your application to make better use of
the computational resources of multi-core machines, you are advised to
use multiprocessing. However, threading is still an appropriate model
if you want to run multiple I/O-bound tasks simultaneously.
Official documentation.
如果您没有任何代码示例,我只能建议将大字典分成几个部分,并使用Pool.map处理每个部分。并在主流程中合并结果。在
不幸的是,在不同的python进程之间有效地共享大量内存是不可能的(我们讨论的不是基于mmap的共享内存模式)。但是你可以在不同的过程中阅读字典的不同部分。或者只需在主进程中阅读整个字典,并给子进程一小块。在
另外,我要提醒你,你应该非常小心地使用多处理算法。因为每多出一兆字节就会乘以进程的数量。在
因此,基于您的伪代码示例,我可以假设基于compute函数的两种可能的算法:# "Stateless&#