问题描述
例如,如果我使用 chain代码>
,我可以在多个线程上调用它吗?请注意,依赖 GIL 的线程安全是可以接受的,但不是可取的.
For instance, if I create an iterator using chain
, can I call it on multiple threads? Note that thread-safety that relies on the GIL is acceptable, but not preferable.
(请注意,这与 这个问题有点不同,后者涉及生成器,而不是迭代器用 C 写的).
(Note that this is a bit different from this question, which deals with generators, not iterators written in C).
推荐答案
首先,有关 itertools 的官方文档 说它们是线程安全的.因此,按照规范,Python 似乎对此没有任何保证.这可能因 Jython 或 PyPy 等实现而异,但这意味着您的代码可能无法移植.
Firstly, nothing in the official documentation on itertools say that they're thread-safe. So it seems that by specification Python does not guarantee anything about that. This might be different across implementations such as Jython or PyPy, but this means your code probably wont be portable.
其次,大多数itertools
(除了简单的,比如count
)都将其他迭代器作为它们的输入.您还需要这些迭代器以线程安全的方式正确运行.
Secondly, most itertools
(with exception of simple ones, like count
) take other iterators as their input. You'd need these iterators to also behave correctly in a thread-safe way.
第三,当不同的线程同时使用时,一些迭代器可能没有意义.例如,在多个线程中工作的 izip
可能会进入竞争条件,从多个来源获取元素,尤其是由等效的 Python 代码定义的(当一个线程设法仅从一个输入迭代器获取值时会发生什么,然后是其中两个的第二个线程?).
Thirdly, some iterators might not make sense when used simultaneously by different threads. For example izip
working in multiple threads might get into race condition taking elements from multiple sources, especially as defined by equivalent python code (what will happen when one thread will manage to take value from only one input iterator, then second thread from two of them?).
还要注意,文档没有提到 itertools
是用 C 实现的.我们知道(作为实现细节)CPython 的 itertools
实际上是用 C 编写的,但是在其他实现中,它们可以愉快地实现为生成器,您可以回到 您引用的问题.
Also note that the documentation does not mention that itertools
are implemented in C. We know (as an implementation detail) that CPython's itertools
are actually written in C, but on other implementations they can happily be implemented as generators, and you can go back to the question you cited.
所以,不,除非您知道目标 Python 平台的实现细节,否则您不能假设它们是线程安全的.
So, no, you cannot assume that they are thread-safe unless you know implementation details of your target python platform.
这篇关于itertools 是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!