Python cStringIO 线程安全吗?
正如标题所说,Python cStringIO 是否保护其内部结构以供多线程使用?
谢谢。
As title say, does Python cStringIO protect their internal structures for multithreading use?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下解释 GIL 的优秀作品,然后注意 cStringIO 是纯粹用 C 编写的,并且它的调用不会释放 GIL。
这意味着正在运行的线程不会在 read()/write() 期间自动切换(使用当前的虚拟机实现)。 (操作系统将抢占该线程,但是其他Python线程将无法获取GIL。)
查看源代码:Python-2.7.1/Modules/cStringIO.c,没有提及内部保护。如有疑问,请查看来源:)
Take a look at an excellent work on explaining GIL, then note that cStringIO is written purely in C, and its calls don't release GIL.
It means that the running thread won't voluntarily switch during read()/write() (with current virtual machine implementation). (The OS will preempt the thread, however other Python threads won't be able to acquire GIL.)
Taking a look at the source: Python-2.7.1/Modules/cStringIO.c there is no mention about internals protection. When in doubt, look at source :)
我假设您正在谈论 Python 的 CPython 实现。
在 CPython 中,有一个全局解释器锁,这意味着一次只能执行一个 Python 代码线程。因此,用 C 编写的代码实际上也是单线程的,除非它显式释放全局锁。
这意味着,如果您有多个 Python 线程同时使用 cStringIO,则不会有任何问题,因为一次只能激活对 cStringIO 方法的单个调用,并且 cStringIO 永远不会释放锁。但是,如果您直接从在锁定环境之外运行的 C 代码调用它,则会遇到问题。此外,如果您执行任何比读取或写入更复杂的操作,您也会遇到问题,例如,如果您开始使用
seek
,因为您的调用可能会以意想不到的方式重叠。另请注意,某些方法(例如
writelines
)可以从方法内部调用 Python 代码,因此在这种情况下,您可能会在对writelines
的单次调用中得到交错的其他输出。对于大多数标准 Python 对象来说都是如此:您可以安全地使用来自多个线程的对象,因为各个操作不会中断,但不会定义事情发生的顺序。
I assume you are talking about the CPython implementation of Python.
In CPython there is a global interpreter lock which means that only a single thread of Python code can execute at a time. Code written in C will therefore also be effectively single threaded unless it explicitly releases the global lock.
What that means is that if you have multiple Python threads all using cStringIO simultaneously there won't be any problem as only a single call to a cStringIO method can be active at a time and cStringIO never releases the lock. However if you call it directly from C code which is running outside the locked environment you will have problems. Also if you do anything more complex than just reading or writing you will have issues, e.g. if you start using
seek
as your calls may overlap in unexpected ways.Also note that some methods such as
writelines
can invoke Python code from inside the method so in that case you might get other output interleaved inside a single call towritelines
.That is true for most of the standard Python objects: you can safely use objects from multiple threads as the individual operations won't break, but the order in which things happen won't be defined.
它与文件操作一样是“线程安全的”(这意味着——不多)。您使用的 Python 实现具有全局解释器锁 (GIL),这将保证每个单独的
cStringIO
上的文件操作不会被另一个线程中断。但这并不能保证来自多个线程的并发文件操作不会交错。It is as "thread-safe", as file operations can be (which means — not much). The Python implementation you're using has Global Interpreter Lock (GIL), which will guarantee that each individual file operation on
cStringIO
will not be interrupted by another thread. That does not however guarantee, that concurrent file operations from multiple threads won't be interleaved.不,它当前不是线程安全的。
No it is not currently thread safe.