Sublime Forum

Can all API functions/methods can be called on a background thread?

#1

The API docs say that all API functions are thread safe, but the unofficial documentation says that “only the set_timeout() function is safe to call from different threads.” Those seem to contradict each other.

I realize that if I’m editing text, I should probably do it on the main thread, so I don’t crash into whatever the user is doing, but is it safe to examine views, open popups, etc., in a background thread (assuming I have proper synchronization)? Or do I need to call set_timeout() to do such things in the main thread?

0 Likes

#2

In Sublime Text 3 all API calls are serialized onto the main thread, so you’re free to call any API you want from any thread in python. Do note that there’s not much reason to use threads in python, due to the GIL.

3 Likes

#3

Thanks, that makes sense. As for using threads, the idea of running a thread in the background is not to make it run faster (it can’t truly run in parallel because of the GIL, as you say), but to prevent blocking the main thread and making sublime unresponsive while some computation is going on.

0 Likes

#4
0 Likes

#5

Yes, threads are a convenient way to have multiple execution streams that don’t block each other even if they contain blocking operations. The GIL is a problem only for CPU-intensive tasks, and there are plenty of ways to overcome it.

0 Likes