Mark Amery
Mark Amery

Reputation: 154954

Which operations in the Sublime Text 2 API are thread-safe?

In the Sublime Text 3 API, all operations are threadsafe:

Threading

All API functions are thread-safe ...

but in the Sublime Text 2 API, this is not the case. As I've personally learned to my displeasure, some operations will, in an unpredictable and OS-dependent way, fail when called from threads other than the main thread with the message

RuntimeError: Must call on main thread, consider using sublime.set_timeout(function, timeout)

I can track down one assurance about thread safety in the Sublime Text 2 docs (emphasis mine):

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.

But wrapping everything in set_timeout calls is tedious and hard to understand. Are there any other API methods I can safely use from off the main thread if I aim to support Sublime Text 2, or is heavy use of set_timeout the only way?

Upvotes: 3

Views: 366

Answers (1)

dano
dano

Reputation: 94931

According to the Sublime Text 3 porting guide, the only thread-safe method in Sublime Text 2 is set_timeout:

In Sublime Text 2, only the set_timeout method was thread-safe. In Sublime Text 3, every API method is thread-safe.

Upvotes: 5

Related Questions