在 Sublime Text 3 API 中,all operations are threadsafe :
Threading
All API functions are thread-safe ...
但在 Sublime Text 2 API 中,情况并非如此。因为我已经personally learned to my displeasure ,当从主线程以外的线程调用消息时,某些操作将以不可预测且依赖于操作系统的方式失败
RuntimeError: Must call on main thread, consider using sublime.set_timeout(function, timeout)
我可以在 Sublime Text 2 文档(强调我的)中找到关于线程安全的保证:
set_timeout(callback, delay)
Calls the given callback after the given delay (in milliseconds). Callbacks with an equal delay will be run in the order they were added. It is safe to call setTimeout from multiple threads.
但是将所有内容包装在 set_timeout
调用中是乏味且难以理解的。如果我的目标是支持 Sublime Text 2,或者大量使用 set_timeout
是唯一的方法,是否还有其他任何我可以从主线程安全地使用的 API 方法?
最佳答案
根据Sublime Text 3 porting guide ,Sublime Text 2 中唯一线程安全的方法是 set_timeout
:
In Sublime Text 2, only the
set_timeout
method was thread-safe. In Sublime Text 3, every API method is thread-safe.
关于multithreading - Sublime Text 2 API 中的哪些操作是线程安全的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975674/